Javascript SetTimeout不延迟函数调用
有人能告诉我为什么下面代码中使用的setTimeout不起作用吗?它只是直接运行函数Javascript SetTimeout不延迟函数调用,javascript,settimeout,Javascript,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 = parseIn
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();
}
感谢您的帮助。按照您的编写方式,就好像
adjust\u for_font(id)
的输出是setTimeout
的第一个参数的输入。第一个参数应该是函数,而不是函数的结果。试试这个
setTimeout(function() {
adjust_for_font(id);
},2000);
问题是这条线
setTimeout(adjust_for_font(id),2000);
这不会计划调用adjust\u for\u font(id)
,而是直接调用函数并计划返回值。要计划函数调用,请将调用包装在lambda中
setTimeout(function() { adjust_for_font(id); },2000);
通过不在函数周围加引号,函数将立即处理,setTimeout将运行(但不会处理函数),而您将不知道到底发生了什么 setTimeout的运行方式如下:
setTimeout('adjust_for_font',2000);
或者在回调中使用匿名函数是另一个选项:
setTimeout(function(){adjust_for_font(id);}, 2000);
改变
setTimeout(adjust_for_font(id),2000);
到
这应该可以做到:
setTimeout(adjust_for_font, 2000, id);
我正在传递函数名,在2000毫秒过去后执行。在您的代码中,您正在传递调整字体的结果。函数名后的括号会导致它在解析后立即执行。这是我的经验。仅指定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
}
SetTimeout语法为SetTimeout(函数,毫秒,参数1,参数2,…) 这里的“函数”不是指函数的调用。它应该是真正的功能 因此,您必须将代码更改为 setTimeout(调整字体的字体,2000,id);(注意:参数id应在毫秒参数之后传递) 或者,您也可以将第一个参数设置为以下参数
setTimeout(函数(){adjust_for_font(id);},2000)
setTimeout(function(){adjust_for_font(id);},2000)
谢谢你的Sim卡!我仍然习惯于javascript——它有时会表现出来!再次感谢。这是一个不错的建议,但在某些版本的safari上仍然不起作用。最好将其设置为函数:setTimeout(函数(){adjust_for_font(id);},2000)代码>并非所有浏览器都支持setTimeout()
的语法,并在延迟后指定函数的参数。在其他答案中使用匿名函数应该可以在所有浏览器中使用。“…让人疑惑到底发生了什么”-是的,这总结了我目前的javascript编程!!这正是我需要的,谢谢!JS是白痴,但这几乎击败了我所见过的一切!虽然…这是我的问题,所以我要谢谢你!
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
}