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. Linux系统——28个命令行下的工具

    Unix/Linux下的28个命令行下的工具 下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些是不 ...

  2. code forces 999C Alphabetic Removals

    C. Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. Java并发编程--CyclicBarrier

    概述 CyclicBarrier是一个同步工具类,它允许一组线程互相等待,直到到达某个公共屏障点.与CountDownLatch不同的是该barrier在释放等待线程后可以重用,所以称它为循环(Cyc ...

  4. Gradle for Android(三)多渠道打包、配置签名信息

    多渠道打包 国内有太多Android App市场,每次发版几十个渠道包.还好Android Gradle给我们提供了productFlavors,我们可以对生成的APK包进行定制. productFl ...

  5. 行为型设计模式之迭代器模式(Iterator)

    结构 意图 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示. 适用性 访问一个聚合对象的内容而无需暴露它的内部表示. 支持对聚合对象的多种遍历. 为遍历不同的聚合结构提供一 ...

  6. 将GDB中的输出定向到文件

    1 set args >output.log 三种方法,一种通过tee在启动时重定向: 1 gdb |tee -a file 第二种在run时加入: 1 run <input.txt &g ...

  7. linux内核分析之进程地址空间【转】

    转自:http://blog.csdn.net/bullbat/article/details/7106094 版权声明:本文为博主原创文章,未经博主允许不得转载. 本文主要介绍linux内核中进程地 ...

  8. 通过过滤器和增强request对象解决get提交请求服务器端乱码。

    1.表单用get方式提交 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  9. PHP获取不带后缀的文件名方法

    $filename = "test.txt"; $houzhui = substr(strrchr($filename, '.'), 1); $result = basename( ...

  10. js/jquery获取元素,元素筛选器

    1.js获取元素 var test = document.getElementById("test"); var parent = test.parentNode; // 父节点 ...