admin管理员组文章数量:1406047
I got a bit confused about a code snippet that I took from the react.js docs: .html
But I converted it to a general js case:
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();
I'm confused by the following line:
this.intervals.push(setInterval.apply(null, arguments));
When I console.log the the argument of push
, it returns 1
.
Can someone explain what actually gets stored in this array?
The cleanup works, so why does the array show the array index number (+1?) instead of what is actually stored in the array?
React.js jsfiddle:
/
I got a bit confused about a code snippet that I took from the react.js docs: http://facebook.github.io/react/docs/reusable-ponents.html
But I converted it to a general js case:
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();
I'm confused by the following line:
this.intervals.push(setInterval.apply(null, arguments));
When I console.log the the argument of push
, it returns 1
.
Can someone explain what actually gets stored in this array?
The cleanup works, so why does the array show the array index number (+1?) instead of what is actually stored in the array?
React.js jsfiddle:
http://jsfiddle/xsjfq5ex/2/
2 Answers
Reset to default 6setInterval
returns the interval ID, so that is what gets stored in your array:
Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function. Returns an intervalID.
intervalID is a unique interval ID you can pass to
clearInterval()
.
You're creating a property called setInterval
on the object whose constructor is SomeFunc
. The property is a function that simply delegates the task of creating an interval to the global setInterval
, but also keeps track of the ones it creates.
Using setInterval.apply(null, arguments)
, you are essentially calling the global setInterval
, and passing the arguments down to that (which would be the function to execute and the interval in milliseconds). The null
indicates what this
will reference inside the body of the function.
setInterval()
will create the interval and return the ID assigned to that interval; this ID is what you are storing in the array. When you call cleanup
, you are mapping the array via clearInterval
, which accepts the ID of the interval to clear (and in this case, is passed in via map
's callback.
So in simple terms, you're creating an intermediate layer to store the intervals as if they're "assigned" to the object you created. This makes them easier to clear when you're finished.
本文标签: javascriptStore setInterval in arrayStack Overflow
版权声明:本文标题:javascript - Store setInterval in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965680a2634933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论