- Initial component start timer (7000 ms).
- IDLE timer, or the default loop timer (5000 ms).
- After Interaction timer (3000 ms), so if you moused over the component, I'd pause the animation but we wanted this quicker interval so the user wasn't left waiting for that potential 14+ seconds...
Now there is no way of modifying the duration of a setInterval() once it's started but we can replicate exactly what is required by using setTimeout and a recursive function. I can think of a couple of ways to achieve this, some perhaps being more generic but here's the code I implemented for this particular component...
com.middleweek.strip.prototype.startDualTimer = function (firstDelay, ongoingDelay) {
"use strict";
var self = this;
if (self._internal.timer === null) { // Only start the timer if it's not already started...
var timeoutHandler = function () {
"use strict";
if (self._internal.timer) { // Check to see if it's been stopped.
self.timerInternalEventHandler();
}
self._internal.timer = window.setTimeout(timeoutHandler, ongoingDelay);
};
self._internal.timer = window.setTimeout(timeoutHandler, firstDelay);
}
}
It's all pretty self-explanatory but here we have a function that accepts two values, firstDelay and ongoingDelay. The first call to setTimeout uses the firstDelay which invokes it's private closure function timeoutHandler() which in turns calls the components timerInternalEventHandler() function to do it's magic and then it continues to recursively call the private closure with ongoingDelay, and so forth...At any time, we can call clearTimeout(self._internal.timer) and then set it to null to cease the magic.
...setInterval's best friend!
No comments:
Post a Comment
The internet is a crazy place so please help us keep it under control by leaving only relevant messages and comments, thanks a bunch! :m)