admin管理员组文章数量:1400579
In javascript when I create an interval, I want to stop the interval from inside the function, but I don't want to reference the ID value from outside like this
var y = setInterval(function(){ clearInterval(y); }, 1000);
What I want is to pass a variable similar to this style
setTimeout(function(data){alert(data);}, 1000, "data");
This works for setInterval too, except I can't really pass the id value that's returned by the setInterval function, because it gets created after calling it.
Right now I'm doing a hack like this:
var r = [];
var y = setInterval(function(r){ if (r.length==1) { clearInterval(r[0]); } }, 1000, r);
r.push(y);
Does anyone know the right way?
Thanks
In javascript when I create an interval, I want to stop the interval from inside the function, but I don't want to reference the ID value from outside like this
var y = setInterval(function(){ clearInterval(y); }, 1000);
What I want is to pass a variable similar to this style
setTimeout(function(data){alert(data);}, 1000, "data");
This works for setInterval too, except I can't really pass the id value that's returned by the setInterval function, because it gets created after calling it.
Right now I'm doing a hack like this:
var r = [];
var y = setInterval(function(r){ if (r.length==1) { clearInterval(r[0]); } }, 1000, r);
r.push(y);
Does anyone know the right way?
Thanks
Share Improve this question asked Mar 10, 2016 at 15:25 omegaomega 44k90 gold badges286 silver badges523 bronze badges 4- The right way seems to be the first piece of code... I don't understand why you want to do this... – elclanrs Commented Mar 10, 2016 at 15:27
- 1 “except I can't really pass the id value that's returned by the setInterval function, because it gets created after calling it” – so what answer besides, “go fire up your time machine!”, do you expect here then …? – C3roe Commented Mar 10, 2016 at 15:27
- … of course you can encapsulate the whole thing into an object (if you’re opposed to having a global variable holding the interval id laying around.) – C3roe Commented Mar 10, 2016 at 15:28
- You don't want to reference the variable from outside -> that's how it was designed you have no choice see w3schools./jsref/met_win_clearinterval.asp. – Walfrat Commented Mar 10, 2016 at 15:31
2 Answers
Reset to default 5You have to use the return value, but that doesn't mean that the variable needs to be accessible to anything else; just encapsulate:
(function() {
var y = setInterval(function(){ clearInterval(y); }, 1000);
})();
y
in the above is only accessible within the outer anonymous function, which only has the interval function and nothing else in it.
You could even give yourself a reusable setInterval
wrapper to do it:
function setIntervalWrapper(callback, time) {
var args = Array.prototype.slice.call(arguments, 1);
args[0] = setInterval(function() {
callback.apply(null, args);
}, time);
}
That will pass the time handle as the first argument, in front of any others you specify. It also has the benefit of supporting those follow-on arguments reliably cross-browser (some older browsers didn't support passing arguments to the callback).
Gratuitous example:
function setIntervalWrapper(callback, time) {
var args = Array.prototype.slice.call(arguments, 1);
args[0] = setInterval(function() {
callback.apply(null, args);
}, time);
}
var counter = 0;
setIntervalWrapper(function(handle, arg1, arg2) {
console.log("Interval callback called, handle is " + handle + ", args are '" + arg1 + "' and '" + arg2 + "'");
if (++counter > 5) {
console.log("counter > 5, stopping");
clearInterval(handle);
}
}, 500, "a", "b");
You can create a new method, which can provide you the desired functionality, like
window.createInterval = function(cb, timeout, data) {
if (typeof cb !== "function") {
throw new Error("Please provide a callback.");
}
var interval = window.setInterval(function(data) {
cb(data, interval);
}, timeout || 0, data);
return interval;
};
Now this works like
var myInterval = window.createInterval(function(data, interval){
console.log(data, interval);
}, 1000, "Hello");
which prints
Hello
and Interval Id (same as myInterval)
window.createInterval = function(cb, timeout, data) {
if (typeof cb !== "function") {
throw new Error("Please provide a callback.");
}
var interval = window.setInterval(function(data) {
cb(data, interval);
}, timeout || 0, data);
return interval;
};
var myInterval = window.createInterval(function(data, interval) {
document.write(data + " " + interval + " " + myInterval);
window.clearInterval(interval);
}, 1000, "Hello");
本文标签: settimeoutHow to pass intervalID to interval function in javascriptStack Overflow
版权声明:本文标题:settimeout - How to pass intervalID to interval function in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744201829a2595000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论