October 29th, 2002, 08:02 PM
I have a really interesting problem on my hands and I don't know what is going on, so any suggestions at all would help me. I was hoping that someone here would be able to help me. Here is my problem:
I have a JS script that looks like this:
var endTime = new Date();
var endSec = Math.round(endTime.getTime() / 1000);
imgout = new Image(1,1);
imgout.src = "file.php?endsec="+ endSec;
All this does is get the current (milli)seconds from 1-1-1970, then it creates a new image where the source of it is my PHP file that triggers some SQL code to update my DB. Normally this works fine (no problems when the file loads (onload)), but as you might have saw, this function is for exit times and is thus triggered with the onunload JS function, like this:
Here is the real mind-bender: For me this function will work up to(and not past) 16 seconds from the intial hit (ie, when the page was loaded). So, in other words, when someone stays on the web page anywhere between 1-16 seconds, everything is fine and it will access my PHP file and update my database, but 17 seconds or more will not work except for some very rare occasions.
Can anyone think of any reason why this would be happening? Is there a fix for it? A different way to try that might work better (or at all)?
Please let me know if you can thing of anything! Thanks.
October 30th, 2002, 07:10 PM
Can anybody think of any solution at all? Anything that might work? A different way? A way to check that the link has been accessed and loaded before the transfer of a page? Or is this just simply not possible to do? Could someone please give me some sort of answer... Thanks.
October 30th, 2002, 07:14 PM
Can you just get the date/time from your php page? It would be the server's time, and not the clients, but you should still be able to get an interval from that.
October 30th, 2002, 08:31 PM
You know, that thought never occured to me... I will have to try it. But I still don't know if the image source would connect after 16 seconds, it doesn't right now (except rare occasions), but maybe, just maybe your suggestion could be the difference. I will let you know if it works or not. Thanks for taking the time to say something about it.
October 30th, 2002, 10:32 PM
Nope, that didn't work. But it did clean my code up a little more... so I guess that I should thank you for that If you can think of anything more that might be of use to me, please let me know.
October 31st, 2002, 04:03 PM
Well, you could try poping up a small window that closes itself quickly...
It's really odd that the image doesn't connect after 16 seconds.
I wonder if that part of the script ever gets executed? Can you pop up an alert at the end to see if it gets reached?
October 31st, 2002, 05:26 PM
I have tried the alert function before, and for some reason, it will record my data at anytime. My guess is that the page is transfering too fast, and thus is not finishing the connection before it changes the page; the alert function delays the transfer just enough to allow the .src to finish the transaction.
I had thought about opening a window also, but for the application that I am building (a system that tracks a user on a web site, built for muiltiple clients each having their own domain, having this code on thier web pages), I can not have JS code doing this every time someone changes a page when using my tracking code. I think that this would piss a few people off, including myself if I had to see a window open and close for every page on my web site if I wanted to track my users.
I have tried to use the image.complete property to test if the .scr has completed loading the image (or in this case a PHP file) but it does not registar for this type of link, only for actual images (.gif, .jpg, etc.).
Also, I have tried to use code like this:
just to try to slow the browser down just a little, but it will only exceute the first loop, then exit the page.
... welcome to my headache. Thanks for your thoughts, let me know if you think of anything else so I can know that I am not going crazy and that I am not the only one baffed by this...
Last edited by nbagley; October 31st, 2002 at 05:31 PM.