Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  142] [ 2]  / answers: 1 / hits: 183423  / 14 Years ago, mon, may 24, 2010, 12:00:00

Normally, I’d set the interval to a variable and then clear it like var the_int = setInterval(); clearInterval(the_int); but for my code to work I put it in an anonymous function:



function intervalTrigger() {
setInterval(function() {
if (timedCount >= markers.length) {
timedCount = 0;
}

google.maps.event.trigger(markers[timedCount], click);
timedCount++;
}, 5000);
};

intervalTrigger();


How do I clear this? I gave it a shot and tried var test = intervalTrigger(); clearInterval(test); to be sure, but that didn’t work.



Basically, I need this to stop triggering once my Google Map is clicked, e.g.



google.maps.event.addListener(map, click, function() {
//stop timer
});

More From » settimeout

 Answers
42

The setInterval method returns a handle that you can use to clear the interval. If you want the function to return it, you just return the result of the method call:



function intervalTrigger() {
return window.setInterval( function() {
if (timedCount >= markers.length) {
timedCount = 0;
}
google.maps.event.trigger(markers[timedCount], click);
timedCount++;
}, 5000 );
};
var id = intervalTrigger();


Then to clear the interval:



window.clearInterval(id);

[#96694] Friday, May 21, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raveno

Total Points: 453
Total Questions: 92
Total Answers: 92

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
raveno questions
;