admin管理员组文章数量:1023098
As far as a I know, there is no way to get setTimeout to run synchronously.
I have this code, where foo is a decently long function (20 lines or so).
if(delay == null){
function foo(){
}()
}
else {
setTimeout(function(){
function foo(){
}()
}, delay);
}
what would very convenient, would be if setTimeout would accept an delay argument of -1, and then would just run synchronously/immediately, something like this:
var delay = delay || -1;
setTimeout(function(){
function foo(){
}()
}, delay); // if delay is -1, will run immediately.
I don't think this functionality works with setTimeout, is there another way I can prevent having to write out the foo function twice? The only good solution I have is to create a helper function, but that's not as convenient :)
As far as a I know, there is no way to get setTimeout to run synchronously.
I have this code, where foo is a decently long function (20 lines or so).
if(delay == null){
function foo(){
}()
}
else {
setTimeout(function(){
function foo(){
}()
}, delay);
}
what would very convenient, would be if setTimeout would accept an delay argument of -1, and then would just run synchronously/immediately, something like this:
var delay = delay || -1;
setTimeout(function(){
function foo(){
}()
}, delay); // if delay is -1, will run immediately.
I don't think this functionality works with setTimeout, is there another way I can prevent having to write out the foo function twice? The only good solution I have is to create a helper function, but that's not as convenient :)
Share Improve this question asked Mar 14, 2016 at 18:59 Alexander MillsAlexander Mills 101k166 gold badges539 silver badges919 bronze badges 1- You could change foo to run accordingly ? Passing a variable in it ? – Harshal Carpenter Commented Mar 14, 2016 at 19:01
4 Answers
Reset to default 3Try this:
function foo(){
}
if(delay == null){
foo();
}
else {
setTimeout(foo, delay);
}
Is there another way I can prevent having to write out the
foo
function twice?
Yep.
Define foo
:
function foo() {
}
And then run it immediately if delay === -1
, otherwise in a setTimeout
:
if (delay === -1)
foo();
else
setTimeout(foo, delay);
This is really old, but you can simply just use an inline condition for the time delay.
setTimeout(foo, isDelay ? delay : 0)
Mention "delay" time at least 1000 milisec.
setTimeout(foo, isDelay ? delay : 1000)
As far as a I know, there is no way to get setTimeout to run synchronously.
I have this code, where foo is a decently long function (20 lines or so).
if(delay == null){
function foo(){
}()
}
else {
setTimeout(function(){
function foo(){
}()
}, delay);
}
what would very convenient, would be if setTimeout would accept an delay argument of -1, and then would just run synchronously/immediately, something like this:
var delay = delay || -1;
setTimeout(function(){
function foo(){
}()
}, delay); // if delay is -1, will run immediately.
I don't think this functionality works with setTimeout, is there another way I can prevent having to write out the foo function twice? The only good solution I have is to create a helper function, but that's not as convenient :)
As far as a I know, there is no way to get setTimeout to run synchronously.
I have this code, where foo is a decently long function (20 lines or so).
if(delay == null){
function foo(){
}()
}
else {
setTimeout(function(){
function foo(){
}()
}, delay);
}
what would very convenient, would be if setTimeout would accept an delay argument of -1, and then would just run synchronously/immediately, something like this:
var delay = delay || -1;
setTimeout(function(){
function foo(){
}()
}, delay); // if delay is -1, will run immediately.
I don't think this functionality works with setTimeout, is there another way I can prevent having to write out the foo function twice? The only good solution I have is to create a helper function, but that's not as convenient :)
Share Improve this question asked Mar 14, 2016 at 18:59 Alexander MillsAlexander Mills 101k166 gold badges539 silver badges919 bronze badges 1- You could change foo to run accordingly ? Passing a variable in it ? – Harshal Carpenter Commented Mar 14, 2016 at 19:01
4 Answers
Reset to default 3Try this:
function foo(){
}
if(delay == null){
foo();
}
else {
setTimeout(foo, delay);
}
Is there another way I can prevent having to write out the
foo
function twice?
Yep.
Define foo
:
function foo() {
}
And then run it immediately if delay === -1
, otherwise in a setTimeout
:
if (delay === -1)
foo();
else
setTimeout(foo, delay);
This is really old, but you can simply just use an inline condition for the time delay.
setTimeout(foo, isDelay ? delay : 0)
Mention "delay" time at least 1000 milisec.
setTimeout(foo, isDelay ? delay : 1000)
本文标签: javascriptConditional setTimeout callStack Overflow
版权声明:本文标题:javascript - Conditional setTimeout call - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745579571a2157229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论