如何清除这个的setInterval [英] How do I clear this setInterval

查看:139
本文介绍了如何清除这个的setInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,ID间隔设置为一个变种,然后清除像 VAR the_int =的setInterval()的变种; clearInterval(the_int); 但是对于我的code工作,我把它放在一个匿名函数:

 函数intervalTrigger(){
    的setInterval(函数(){
        如果(timedCount&GT = markers.length){timedCount = 0;}
        google.maps.event.trigger(标记[timedCount],点击);
        timedCount ++;
    },5000);
};
intervalTrigger();

如何清除呢?我曾尝试 VAR测试= intervalTrigger(); clearInterval(测试); ,但没有工作。其中,我预期没有工作,但只是给它一个镜头。

基本上,我需要我的一次谷歌地图点击该停止引发......例如

  google.maps.event.addListener(地图,'点击',功能(){
    //停止计时器
});


解决方案

的setInterval 方法返回的句柄,您可以使用清除的时间间隔。如果你想在函数返回它,你只返回方法调用的结果是:

 函数intervalTrigger(){
  返回window.setInterval(函数(){
    如果(timedCount> = markers.length){
       timedCount = 0;
    }
    google.maps.event.trigger(标记[timedCount],点击);
    timedCount ++;
  },5000);
};
变种的id = intervalTrigger();

然后清除间隔:

  window.clearInterval(ID);

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
});

解决方案

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);

这篇关于如何清除这个的setInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆