SetTimeout不延迟函数调用 [英] SetTimeout not delaying a function call

查看:79
本文介绍了SetTimeout不延迟函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么在下面的代码中使用的setTimeout不起作用?它只是直接运行该功能。

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();
}

感谢任何帮助。

推荐答案

问题是这一行

setTimeout(adjust_for_font(id),2000);

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

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

这篇关于SetTimeout不延迟函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆