#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    27
    Rep Power
    0

    Exclamation To All JavaScript Junkies and Pros: Help!


    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:

    function exitTime(){
    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:

    <body onunload="exitTime()">

    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.

    nbagley
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    27
    Rep Power
    0
    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.
  4. #3
  5. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    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.
    -james
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    27
    Rep Power
    0
    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.

    nbagley
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    27
    Rep Power
    0
    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.

    nbalgey
  10. #6
  11. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    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?
    -james
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    27
    Rep Power
    0
    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 think that the solution would be to delay the transfer of the page (even if it was a 1/4 of a second or less) or to make sure that the image source has completed the function before transfering. Do you know if you can delay the transfer of a page by a javascript function?

    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:

    PHP Code:
    <script>

    for(
    i=0;i<4500;i++){}

    </script> 
    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...

    nbagley
    Last edited by nbagley; October 31st, 2002 at 04:31 PM.

IMN logo majestic logo threadwatch logo seochat tools logo