From 305d6259bcfee32ffa9359e0a2766a867927fa91 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 2 Nov 2011 23:35:01 -0400 Subject: [PATCH] Bug 697250. When an interval is very late, just go ahead and schedule it for firing ASAP and update its target time to 'now' (the latter to prevent misbehavior when the interval is very late due to the computer being suspended, say) instead of trying to schedule it for a while from now. r=jst --- dom/base/nsGlobalWindow.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 40e91e056e6a..8f719e1edd6c 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -9366,25 +9366,27 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout) // If we're running pending timeouts because they've been temporarily // disabled (!aTimeout), set the next interval to be relative to "now", - // and not to when the timeout that was pending should have fired. Also - // check if the next interval timeout is overdue. If so, then restart - // the interval from now. + // and not to when the timeout that was pending should have fired. TimeStamp firingTime; - if (!aTimeout || timeout->mWhen + nextInterval <= now) + if (!aTimeout) firingTime = now + nextInterval; else firingTime = timeout->mWhen + nextInterval; - TimeDuration delay = firingTime - TimeStamp::Now(); + TimeStamp currentNow = TimeStamp::Now(); + TimeDuration delay = firingTime - currentNow; // And make sure delay is nonnegative; that might happen if the timer - // thread is firing our timers somewhat early. + // thread is firing our timers somewhat early or if they're taking a long + // time to run the callback. if (delay < TimeDuration(0)) { delay = TimeDuration(0); } if (timeout->mTimer) { - timeout->mWhen = firingTime; + timeout->mWhen = currentNow + delay; // firingTime unless delay got + // clamped, in which case it's + // currentNow. // Reschedule the OS timer. Don't bother returning any error // codes if this fails since the callers of this method