admin管理员组文章数量:1314558
I'm looking to pass the handler of setInterval()
to the internal function.
For example:
var id = setInterval(myfunction, "100000");
I'm looking to pass id
to myfunction
I tried
setInterval(myfunciton(e), "10000");
but it didn't pass the variable.
Any ideas?
I'm looking to pass the handler of setInterval()
to the internal function.
For example:
var id = setInterval(myfunction, "100000");
I'm looking to pass id
to myfunction
I tried
setInterval(myfunciton(e), "10000");
but it didn't pass the variable.
Any ideas?
Share Improve this question edited Aug 10, 2015 at 20:03 Hannes Johansson 1,8022 gold badges16 silver badges28 bronze badges asked Aug 10, 2015 at 19:46 axwegeaxwege 8611 bronze badges 1- Unless I misunderstood you, you can't pass a parameter that isn't even defined yet... – Maria Ines Parnisari Commented Aug 10, 2015 at 19:48
2 Answers
Reset to default 12The id doesn't exist when you call setInterval
, but it does exist by the time that the callback is called.
You would need to wrap the call in a function that uses the id in a call:
var id = setInterval(function(){
myfunction(id);
}, 10000);
If you include the variable in the callback as it's passed in to setInterval, the callback is immediately executing with that argument.
What you need to do is create a closure:
setInterval(function() {
myFunction(e);
}, 10000);
Also, the interval should be an integer, not a string.
本文标签: javascriptHow to pass ID of setInterval() to anonymous funtionStack Overflow
版权声明:本文标题:javascript - How to pass ID of setInterval() to anonymous funtion - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741967525a2407636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论