将setInterval存储在数组中

Tra*_*ace 2 javascript

我对于我从react.js文档中获取的代码片段感到有些困惑:http://facebook.github.io/react/docs/reusable-components.html

但我把它转换成一般的js案例:

var SomeFunc = function(){
    this.intervals = []; 
    this.setInterval = function(){
        this.intervals.push(setInterval.apply(null, arguments));
    }; 
    var self = this; 
    this.cleanup = setTimeout(function(){ 
        self.intervals.map(clearInterval);
    }, 3000); 
}

var someFunc = new someFunc(); 
someFunc.setInterval(this.tick, 1000); 
someFunc.cleanup();  
Run Code Online (Sandbox Code Playgroud)

我对以下几行感到困惑:

this.intervals.push(setInterval.apply(null, arguments));
Run Code Online (Sandbox Code Playgroud)

当我在console.log中输入参数时push,它返回1.
有人可以解释实际存储在这个数组中的内容吗?
清理工作,那么为什么数组显示数组索引号(+ 1?)而不是实际存储在数组中的数?

React.js jsfiddle:http:
//jsfiddle.net/xsjfq5ex/2/

moo*_*e99 6

setInterval 返回间隔ID,这是存储在数组中的内容:

重复调用函数或执行代码片段,每次调用该函数之间都有固定的时间延迟.返回intervalID.

intervalID是您可以传递给的唯一间隔ID clearInterval().

  • 谢谢,就是这样.几分钟后接受. (2认同)