How to Clear setInterval() without Knowing the ID
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的更多相关文章
- js学习--浏览器对象计时器setInterval()与setTimeout()的使用与区别
一.setInterval()与setTimeout()的定义: 二.setInterval()与setTimeout()的使用: 1.setInterval()与clearInterval() ...
- 跨JavaScript对象作用域调用setInterval方法
跨JavaScript对象作用域调用setInterval方法: var id = window.setInterval(function() {foofunc.call(this);}, 200);
- 关于clearfix和clear的讨论
本文摘自百度文库 还是提到了一个关于网页制作很古老的问题,浮动的清除. 虽然看过一些资料介绍说能不用浮动就尽量不要用,但对定位不是很熟的我来说,浮动就不能不用了:既然惹上这个麻烦,就得想个办法进行解决 ...
- setInterval(),setTimeout(),location.reload(true)
1,setInterval() setInterval()方法可以按照指定的周期来调用函数或表达式,他会不停地调用函数,直到调用clearInterval()方法或窗口关闭.由setInterval( ...
- JavaScript之setInterval() 函数
定义和用法 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被 ...
- setInterval 与 clearInterval详解
首先注意,setInterval与clearInterval都是直属于window对象的. 1.直接调用setInterval(即不通过函数调用) <div id="oDiv_show ...
- css中的clear:both,display:flex;
介绍两者一起讨论的原因: 在明天就国庆的日子里陪着程序员的只有代码,啤酒,还有音乐,然后就是灯光下默默陪伴自己的影子.好了,不矫情了. -------------------------------- ...
- CSS清除浮动常用方法小结 CSS clear both {overflow:auto;zoom:1;}
常用的清除浮动的方法有以下三种: 此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景 <!DOCTYPE html><html><head> <met ...
- JS不间断向上滚动 setInterval和clearInterval
<div id=demo style=overflow:hidden;height:139;width:232;background:#f4f4f4;color:#ffffff><d ...
随机推荐
- 转:LinkedHashMap使用(可以用来实现LRU缓存)
1. LinkedHashMap概述: LinkedHashMap是HashMap的一个子类,它保留插入的顺序,如果需要输出的顺序和输入时的相同,那么就选用LinkedHashMap. LinkedH ...
- python urllib2 常见请求方式
GET 添加headers头import urllib2 request = urllib2.Request(uri) request.add_header('User-Agent', 'fake-c ...
- xen hypercall 的应用层实现
一句话描述: xen hypercall 在应用层的实现,最终都变成对 /proc/xen/privcmd 的 ioctl 系统调用 我们知道,xen 在应用层最上层的接口是 libxl , 基本上 ...
- python cProfile分析程序性能
转自:http://xianglong.me/article/analysis-python-application-performance-using-cProfile/?utm_source=tu ...
- (sql server)玩转-数据库行列转换
虽然开发过程中没用过行列转换,但是听说面试时常常会遇到这个问题,以前在网上也看到过大神的例子,今天自己仔细的玩了下,希望和大家分享一下了. 注意:列转行的方法可能是我独创的了,呵呵,因为在网上找不到哦 ...
- 第十二届北航程序设计竞赛决赛网络同步赛 B题 前前前世(数论推导 + DP)
题目链接 2016 BUAA-Final Problem B 考虑一对可行的点$(x, y)$ 根据题意,设$x = ak + 1,y = bk + 1$ 又因为$x$是$y$的祖先的祖先的祖先,所 ...
- Python与正则表达式[0] -> re 模块的正则表达式匹配
正则表达式 / Regular Expression 目录 正则表达式模式 re 模块简介 使用正则表达式进行匹配 正则表达式RE(Regular Expression, Regexp, Regex) ...
- Python的程序结构[4] -> 函数/Function[2] -> 匿名函数
匿名函数 / Anonymous Function 匿名函数是一种不需要绑定函数名的函数 (i.e. functions that are not bound to a name).匿名函数通过 la ...
- Ext分区文件恢复工具extundelete
Ext分区文件恢复工具extundelete Ext是延伸文件系统(Extended system)的缩写.它是为Linux内核开发的第一个文件系统.它有多个版本.现在常见的是Ext3和Ext4.由 ...
- rest ---hateoas
推荐一篇博客. https://www.ibm.com/developerworks/cn/java/j-lo-SpringHATEOAS/