Possible Duplicate:
How to pause a setTimeout call ?
I have a function that gets called on page load which starts off a repeating function:
setTimeout(function () {
repeat();
}, 8000)
This function calls repeat() every 8 seconds, inside this function I have a bit of ajax which updates a counter on the page. Clicking on the counter gives the user a drop down menu with a number of messages. The counter value equals the number of messages the user has. Kind of like Facebook notifications.
When clicking the drop down menu Im using jQuery to hide and show it:
$('#messages').click(function () {
$('#messagesDropDown').slideDown();
})
.mouseleave(function () {
$('#messagesDropDown').slideUp();
});
When the #messagesDropDown is visible I want to stop the repeat() function, to prevent the list of messages from updating while Im viewing the current ones.
On .mouseleave I want to start the repeat() function again.
Anyone have any ideas how I can ‘STOP’ a repeating function In the .click function and start it again on .mouseleave ?
You said that this code starts a repeating function:
Since
setTimeoutdoesn’t repeat, I assume that therepeatfunction itself fires off anothersetTimeoutto call itself again after it runs (chainedsetTimeoutcalls).If so, you have two options:
Have a control variable telling
repeatwhether to do its work or not. A simple boolean will do. Set the boolean when you wantrepeatto skip its work, and haverepeatcheck it. This is the dead simple answer.Have control functions for
repeat, like so:…and then use them to control the repeats. Be sure to modify
repeatto callstartRepeatto schedule its next call rather than callingsetTimeoutdirectly.