Problem
Declaring a setInterval() without keeping a reference to it (which is returned from the function call setInterval(), it returns an id number for the registered event).

Like so:

var interval = setInterval(function() {
console.log('i');
}, 1000);
console.log(interval);

It you can see it assigns a random number as the id for the event. Then when we clear the interval it uses that id.

 

Simply by calling:

clearInterval(interval);

A Diirty Solution
Now this is not good coding practice (for a number of reasons) but if you really need to clear that interval we can simply do a loop to find the id of the event to stop all setInterval() events that are running. I’ve done a quick jsfiddle to show you. https://jsfiddle.net/WqCws/.

for(i=0; i<100; i++)
{
window.clearInterval(i);
}

I’m pretty sure this will also work for setTimeout() and clearTimeout();

var $timer = $('#timer'),
count = 0; //set the interval
var interval = setInterval(function() {
$timer.html(count++);
}, 1000);
console.log('the interval is: '+interval); //clear it
$('button').bind('click', function(e) {
var found;
for(i=0; i<10000; i++)
{
window.clearInterval(i);
}
});
 

How to Clear setInterval() without Knowing the ID的更多相关文章

  1. js学习--浏览器对象计时器setInterval()与setTimeout()的使用与区别

    一.setInterval()与setTimeout()的定义: 二.setInterval()与setTimeout()的使用:    1.setInterval()与clearInterval() ...

  2. 跨JavaScript对象作用域调用setInterval方法

    跨JavaScript对象作用域调用setInterval方法: var id = window.setInterval(function() {foofunc.call(this);}, 200);

  3. 关于clearfix和clear的讨论

    本文摘自百度文库 还是提到了一个关于网页制作很古老的问题,浮动的清除. 虽然看过一些资料介绍说能不用浮动就尽量不要用,但对定位不是很熟的我来说,浮动就不能不用了:既然惹上这个麻烦,就得想个办法进行解决 ...

  4. setInterval(),setTimeout(),location.reload(true)

    1,setInterval() setInterval()方法可以按照指定的周期来调用函数或表达式,他会不停地调用函数,直到调用clearInterval()方法或窗口关闭.由setInterval( ...

  5. JavaScript之setInterval() 函数

    定义和用法 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被 ...

  6. setInterval 与 clearInterval详解

    首先注意,setInterval与clearInterval都是直属于window对象的. 1.直接调用setInterval(即不通过函数调用) <div id="oDiv_show ...

  7. css中的clear:both,display:flex;

    介绍两者一起讨论的原因: 在明天就国庆的日子里陪着程序员的只有代码,啤酒,还有音乐,然后就是灯光下默默陪伴自己的影子.好了,不矫情了. -------------------------------- ...

  8. CSS清除浮动常用方法小结 CSS clear both {overflow:auto;zoom:1;}

    常用的清除浮动的方法有以下三种: 此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景 <!DOCTYPE html><html><head> <met ...

  9. JS不间断向上滚动 setInterval和clearInterval

    <div id=demo style=overflow:hidden;height:139;width:232;background:#f4f4f4;color:#ffffff><d ...

随机推荐

  1. 转:mysql group by

    group by 用法解析 group by语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. SELECT子句中的列名必须为分组列或列函数.列函数对于GROUP BY子 ...

  2. 百度之星初赛(A)——T6

    度度熊的01世界 Problem Description 度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成. 现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者 ...

  3. 关于0x*** 十六进制的运算。为什么枚举多用十六进制的运算原因。。

    1.看个人爱好 2.可以看出布尔运算的结果. 3.可以更快进行and和or 运算

  4. jquery 追加元素的方法(append prepend after before 的区别)

    append() 方法在被选元素的结尾插入内容. prepend() 方法在被选元素的开头插入内容. after() 方法在被选元素之后插入内容. before() 方法在被选元素之前插入内容. &l ...

  5. authencated认证登录

    #coding:utf-8 import tornado.httpserver import tornado.ioloop import tornado.options import tornado. ...

  6. 推荐一本书:《UML面向对象建模基础》

    http://www.cnblogs.com/onlytiancai/archive/2006/10/13/528205.html 以前对UML呀,感觉用不上,不知道都干啥的,也就是知道有个用例图.类 ...

  7. UVALIVE 3031 Cable TV Network

    题意:求点联通度 首先看了别人的题解还是不晓得只枚举汇点的原因觉得行不通 关于求点联通度的建图方法 转自http://hi.baidu.com/lerroy312/item/5a5f36f2f5bba ...

  8. 关闭vs警告

    禁用所有编译器警告 当“解决方案资源管理器”中有项目选中时,在“项目”菜单上单击“属性”. 单击“编译”选项卡. 选中“禁用所有警告”复选框. 禁用单个编译器警告 在“解决方案资源管理器”中选定一个项 ...

  9. Zabbix监控服务器异常IP登录

    保存下面的脚本到任意路径 名字.py 修改属组和权限: chown zabbix:zabbix * chmod 755 * 演示的路径为:/usr/local/zabbix/scripts/ # -* ...

  10. Python-mysql索引

    MySQL索引的概念 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询 ...