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 ...
随机推荐
- [LeetCode169]Majority Element
Majority Element Total Accepted: 58500 Total Submissions: 163658My Submissions Question Solution Gi ...
- bzoj1266 [AHOI2006]上学路线route floyd+最小割
1266: [AHOI2006]上学路线route Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2490 Solved: 898[Submit][S ...
- Python实现队列
队列的数据结构的主要结构:一个结点类和两个方法:出队列和进队列 class Node(object): def __init__(self,val): self.val = val self.next ...
- 一种提高Android应用进程存活率新方法
一.基础知识 1.Android 进程优先级 1.1 进程优先级等级一般分法:- Activte process- Visible Process- Service process- Backgrou ...
- ef unitofwork 主从表更新
readonly UnitOfWork _u = new UnitOfWork(); public M Get(int id) { return _u.T_MtnContractRepository( ...
- css样式小框架
1.如div{...}会给所有的<div></div>增加样式. 2.名前井号“#”:对应html中的标签的id属性,写法为#name.如#p1{...}会给<p id= ...
- 杭电oj2012-2021
2012 素数判定 #include <stdio.h> #include <math.h> int main() { int x,y,i,j,a,flag; while(s ...
- LINK : fatal error LNK1104: 无法打开文件“mfc71.lib”(转)
原文转自 http://blog.csdn.net/mxclxp/article/details/8196142 [环境] Windows XP OS: Visual Studio 2008: ...
- popen的用法及与system调用的区别
首先用man查看下popen的介绍: popen(3) - Linux man page Name popen, pclose - pipe stream to or from a process S ...
- hdu 5158(水题)
Have meal Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...