javascript SetTimeout 不会延迟函数调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9184702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SetTimeout not delaying a function call
提问by Mark_54
Can somebody please tell me why the setTimeout used in the code below isn't working? It just runs the function straightaway.
有人可以告诉我为什么下面代码中使用的 setTimeout 不起作用吗?它只是直接运行该功能。
function change_txt_font(elem, id, text_fnt){
current_width = parseInt($('#span_text'+id).css('width'));
current_height = parseInt($('#span_text'+id).css('height'));
current_font_size = parseInt($("#span_text"+id).css("font-size"));
parent.document.getElementById(elem+'_f').value=text_fnt;
$('#span_text'+id).css('font-family',text_fnt);
$('#'+elem).css('font-family',text_fnt);
setTimeout(adjust_for_font(id),2000);
}
function adjust_for_font(id){
alert("function")
alert("id = "+id)
new_height = parseInt($('#span_text'+id).css('height'));
new_width = parseInt($('#span_text'+id).css('width'));
width_ratio = parseFloat(current_width/new_width)
height_ratio = parseFloat(current_height/new_height)
new_font_size = current_font_size * Math.min(width_ratio,height_ratio)
$("#text"+id).css("font-size", (parseFloat(new_font_size) - 1) + "px");
$("#span_text"+id).css("font-size", (parseFloat(new_font_size) - 1) + "px");
document.getElementById("form_front_text"+id).submit();
}document.getElementById("form_front_text"+id).submit();
}
Any help appreciated.
任何帮助表示赞赏。
回答by JaredPar
The problem is this line
问题是这条线
setTimeout(adjust_for_font(id),2000);
This doesn't schedule the invoking of adjust_for_font(id)
but instead invokes the function directly and schedules the return value. To schedule the invocation of the function wrap the call in a lambda
这不会调度调用,adjust_for_font(id)
而是直接调用函数并调度返回值。要安排函数的调用,请将调用包装在 lambda 中
setTimeout(function() { adjust_for_font(id); },2000);
回答by Francis Lewis
By not putting quotes around your function, the function will process immediately, setTimeout will run (but won't process a function) and you're left wondering what on earth happened.
通过不在您的函数周围加上引号,该函数将立即处理,setTimeout 将运行(但不会处理函数),您会想知道到底发生了什么。
setTimeout is designed to run like this:
setTimeout 被设计成这样运行:
setTimeout('adjust_for_font',2000);
Or a using an anonymous function in the callback is another option:
或者在回调中使用匿名函数是另一种选择:
setTimeout(function(){adjust_for_font(id);}, 2000);
回答by Michael Robinson
Change
改变
setTimeout(adjust_for_font(id),2000);
to
到
setTimeout("adjust_for_font(id)",2000);
回答by osahyoun
This should do the trick:
这应该可以解决问题:
setTimeout(adjust_for_font, 2000, id);
I am passing the function name, to be executed when 2000 milliseconds have passed. In your code, you are passing the result of adjust_for_font. The brackets after the function name cause it to be executed as soon as it is parsed (immediately).
我正在传递函数名称,在 2000 毫秒过去后执行。在您的代码中,您传递的是 adjust_for_font 的结果。函数名后面的括号使其在解析后立即执行(立即)。
回答by T. Stone
The way you have it written, it's as if the output of adjust_for_font(id)
is the input to the first parameter of setTimeout
. The first parameter should be the function, not the result of the function. Try this instead...
按照您的编写方式,就好像 的输出adjust_for_font(id)
是 的第一个参数的输入setTimeout
。第一个参数应该是函数,而不是函数的结果。试试这个...
setTimeout(function() {
adjust_for_font(id);
},2000);
回答by Hiran
The SetTimeout syntax is setTimeout(function,milliseconds,param1,param2,...)
SetTimeout 语法是 setTimeout(function,milliseconds,param1,param2,...)
Here "function" means not the calling of the function. It should be the real function.
这里的“函数”不是指函数的调用。它应该是真正的功能。
So you have to change your code to
所以你必须改变你的代码
setTimeout(adjust_for_font,2000,id); (note: The parameter id should pass after the milliseconds parameter)
setTimeout(adjust_for_font,2000,id); (注意:参数id应该在milliseconds参数之后传递)
or alternatively you can set the first parameter as bellow
或者您可以将第一个参数设置如下
setTimeout(function() { adjust_for_font(id); },2000);
setTimeout(function() { adjust_for_font(id); },2000);
回答by Ravi Sankar H
This is from my experiences. Just specifying setTimeout() will cause it to execute upon page load itself, even if it's parent function is not called. making it into a variable like this works..
这是根据我的经验。仅指定 setTimeout() 将导致它在页面加载本身时执行,即使它的父函数没有被调用。把它变成一个像这样的变量..
before
前
function xyz(){
//do something if needed
setTimeout(function abc, duration);
//setTimeout will be executed even before xyz() call
}
after
后
function xyz(){
//do something if needed
var t=setTimeout(function abc, duration);
//this time, setTimeout will be executed upon xyz() call and not upon pageload
}