SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Taipei, Taiwan
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Timers/Intervals

    I am trying to add countdown messages on my website using timers/intervals.

    Each countdown message has its own function that needs to be called every second, but I am having a problem with the timing.

    When there is just one timer, the function is called properly, each and every second.

    However, when I add another timer on the same web page, the time interval is no longer 1 second, but closer to half a second.

    How can I solve this problem?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's odd. Try giving each timer a different timer ID if you haven't already.
    Code:
    var timer1 = window.setInterval(func1, 1000);
    var timer2 = window.setInterval(func2, 1000);

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Taipei, Taiwan
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, I did that already, but it still has the same result.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have more than one interval you need to coodinate the times they begin 'counting'.

    This is often done by looking at some common start time and calling each setInterval from
    its own setTimeout, that starts the next Interval at a predetermined time.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Taipei, Taiwan
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    If you have more than one interval you need to coodinate the times they begin 'counting'.

    This is often done by looking at some common start time and calling each setInterval from
    its own setTimeout, that starts the next Interval at a predetermined time.
    That got me confused

    Could you give a simple example?

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What browser are you using? There could be some bugginess associated with it, something to do with garbage collection ( http://ejohn.org/blog/analyzing-timer-performance/ , http://jsninja.com/Timers ), though half a second seems awfully long, especially with only 2 simultaneous timers.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Taipei, Taiwan
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using Firefox 2.0.0.12.

    The timer isn't lagging, actually it's speeding up, with the time interval halved when I use two timers. It might become 3 times faster if I add 3 timers?

    Other users on my website also report this problem, so I don't think it's just me, although most of my users use firefox as well.

  8. #8
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are using the same time interval- like once per second- call all the functions you want to run once per second from the same timer.

    javascript is single threaded, two events or two functions can't run at the same time.
    They get queued, and if the functions are the sort that cause the browser to refresh part of the page, the queue gets longer, and the display, choppy.

    The queued calls return in succession- the intervals that originally called their events are past.

  9. #9
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zegg90 View Post

    However, when I add another timer on the same web page, the time interval is no longer 1 second, but closer to half a second.

    How can I solve this problem?
    A good start would be to show all the relevant code.

    FireFox has a problem with setInterval, in that if two intervals of exactly the same duration are set, they conflict with each other one or the other will fail erratically.
    Tab-indentation is a crime against humanity.


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
  •