views:

103

answers:

1

Normally, id set the interval to a var and then clear the var 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 did try var test = intervalTrigger();clearInterval(test); but that didnt work. Which, i expected it not to work, but just gave it a shot.

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

google.maps.event.addListener(map, 'click', function() {
    //stop timer
});
+4  A: 

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);
Guffa
Awesome you rock! Thanks!
Oscar Godson