SetTimeout 不延迟函数调用
有人可以告诉我为什么下面代码中使用的 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();
}
任何帮助表示赞赏。
Can somebody please tell me why the setTimeout used in the code below isn't working? It just runs the function straightaway.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(7)
猫烠⑼条掵仅有一顆心2025-01-10 00:43:48
这应该可以解决问题:
setTimeout(adjust_for_font, 2000, id);
我传递函数名称,在 2000 毫秒过去后执行。在您的代码中,您传递了 adjustment_for_font 的结果。函数名后面的括号使其在解析后立即执行(立即)。
Saygoodbye2025-01-10 00:43:48
按照您的编写方式,就好像 adjust_for_font(id)
的输出是 setTimeout
第一个参数的输入。第一个参数应该是函数,而不是函数的结果。试试这个吧...
setTimeout(function() {
adjust_for_font(id);
},2000);
转角预定愛2025-01-10 00:43:48
这是我的经历。只需指定 setTimeout() 就会导致它在页面加载时执行,即使它的父函数没有被调用。将其变成这样的变量可以工作..
之前
function xyz(){
//do something if needed
setTimeout(function abc, duration);
//setTimeout will be executed even before xyz() call
}
之后
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
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
问题是这行代码
不会调度
adjust_for_font(id)
的调用,而是直接调用该函数并调度返回值。要安排函数的调用,请将调用包装在 lambda 中The problem is this line
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