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 ...
随机推荐
- spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。
spring 中scope的值有四个:分别是:singleton.prototype.session.request.其中session和request是在web应用中的. 下面证明当scope为pr ...
- spring in action 学习笔记四:bean的生命周期
bean 的生命周期分为:一个是ApplicationContext的容器的bean的生命周期,另一个是BeanFactory容器的生命周期. 首先介绍一下:ApplicationContext的容器 ...
- MFC 在窗口上画指定大小的ICON
CPaintDC dc(this); HICON hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON) ...
- VMware DRS概述及功能
通过动态分配和平衡计算资源,使您的 IT 基础架构与业务目标一致.VMware Distributed Resource Scheduler (DRS) 可持续监控所有资源池的利用率,并根据业务需求在 ...
- JAVA下几个问题
一.Server MyEclipse Tomcat v8.5 was unable to start within 45 seconds. If the server requires more ti ...
- http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/
转载自:http://stormzhang.com/opensource/2016/06/26/android-open-source-project-recommend1/ 推荐他的所有博文~ 图片 ...
- pywordfrom
http://files.cnblogs.com/files/zhang-pengcheng/pywordform-0.02.zip Win8.1自带微软五笔输入法开启方法
- C++ emplace_back
在C++开发过程中,我们经常会用STL的各种容器,比如vector,map,set等,这些容器极大的方便了我们的开发.在使用这些容器的过程中,我们会大量用到的操作就是插入操作,比如vector的pus ...
- SpringMvc+Spring+Mybatis+Maven整合
一.建立数据库表,使用generator自动生成相关代码: /* SQLyog Ultimate v11.24 (32 bit) MySQL - 5.1.62-community : Database ...
- Python学习杂记_15_正则表达式
正则表达式 正则表达式就是用来查找字符串的,它能够查找规则比较复杂的字符串.使用正则表达式首先要导入re模块import re s = "besttest is good!besttest ...