SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SetTimeout - need to check the timer's current position

    Greetings,

    I have a situation with setTimeout where I need to check the number of milliseconds remaining so that the program logic can decide whether to follow through with the operation or delay it a few more seconds.

    Pretty much this is used to reduce lag on the user's end so that if they are in the middle of typing it won't execute the timed function unless they have stopped typing for a few seconds, making it safe to perform the operation (in the background) without interfering with their user experience.

    In any event, there doesn't seem to be a native way to check the current progress or remaining millisecond count on a setTimeout. That is, unless I just haven't been able to find it.

    Thanks!
    David

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know of any way to do that.
    I think you'll just have to use something like...

    // on some user event, store the start time
    var d = new Date();
    var startTime = d.getTime();

    // later, calculate the elapsed time
    var d = new Date();
    var elapsed = d.getTime() - startTime;

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I was able to set up a second timer reset on every keystroke for a couple of seconds. This second timer resets a flag and while the flag is still active, it delays the first timer and sets it to 2 seconds if it tries to execute. Works like a charm.

    It would still be neat if there was a way to check the timer value directly. I imagine there are applications for this. How hard would it have been for them to add that function to javascript? Hmmm....

    David


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •