SetTimeout 不延迟函数调用

发布于 01-03 00:43 字数 1220 浏览 4 评论 0原文

有人可以告诉我为什么下面代码中使用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寄人书2025-01-10 00:43:48

问题是这行代码

setTimeout(adjust_for_font(id),2000);

不会调度 adjust_for_font(id) 的调用,而是直接调用该函数并调度返回值。要安排函数的调用,请将调用包装在 lambda 中

setTimeout(function() { adjust_for_font(id); },2000);

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

setTimeout(function() { adjust_for_font(id); },2000);
乖乖2025-01-10 00:43:48

如果不在你的函数周围加上引号,函数将立即处理,setTimeout 将运行(但不会处理函数),你会想知道到底发生了什么。

setTimeout 被设计为这样运行:

setTimeout('adjust_for_font',2000);

或者在回调中使用匿名函数是另一种选择:

setTimeout(function(){adjust_for_font(id);}, 2000);

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 is designed to run like this:

setTimeout('adjust_for_font',2000);

Or a using an anonymous function in the callback is another option:

setTimeout(function(){adjust_for_font(id);}, 2000);
回梦2025-01-10 00:43:48

更改

setTimeout(adjust_for_font(id),2000);

setTimeout("adjust_for_font(id)",2000);

Change

setTimeout(adjust_for_font(id),2000);

to

setTimeout("adjust_for_font(id)",2000);

这应该可以解决问题:

setTimeout(adjust_for_font, 2000, id);

我传递函数名称,在 2000 毫秒过去后执行。在您的代码中,您传递了 adjustment_for_font 的结果。函数名后面的括号使其在解析后立即执行(立即)。

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).

Saygoodbye2025-01-10 00:43:48

按照您的编写方式,就好像 adjust_for_font(id) 的输出是 setTimeout 第一个参数的输入。第一个参数应该是函数,而不是函数的结果。试试这个吧...

setTimeout(function() {
    adjust_for_font(id);
},2000);

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...

setTimeout(function() {
    adjust_for_font(id);
},2000);
盗心人2025-01-10 00:43:48

SetTimeout 语法为 setTimeout(function,milliseconds,param1,param2,...)

这里的“function”并不是指函数的调用。应该是真正的功能。

所以你必须将代码更改为

setTimeout(adjust_for_font,2000,id); (注意:参数 id 应在毫秒参数之后传递)

或者您可以将第一个参数设置为如下

setTimeout(function() { adjustment_for_font(id); },2000);

The SetTimeout syntax is 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)

or alternatively you can set the first parameter as bellow

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
}

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..

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文