Javascript setTimeout

时间:2010-11-08 22:52:44

标签: javascript

你能告诉我为什么会这样吗:

PageMethods.UpdateForcedDisposition(forcedDisposition, a.value, SucceededCallback, FailedCallback);

什么时候不这样?

setTimeout("PageMethods.UpdateForcedDisposition(" + forcedDisposition + "," + a.value + ", SucceededCallback, FailedCallback);", 1000);

有趣的是,类似的调用适用于setTimeout

setTimeout("PageMethods.UpdateSales(" + id + ", " + a.value + ", SucceededCallback, FailedCallback);", 1000);

......我很难过!

1 个答案:

答案 0 :(得分:6)

避免将字符串传递给setTimeout。尽可能使用匿名函数:

window.setTimeout(function () {
    PageMethods.UpdateForcedDisposition(
        forcedDisposition, 
        a.value, 
        SucceededCallback, 
        FailedCallback
    );
}, 1000);

带有字符串的setTimeout在全局范围内执行。如果您尝试从当前范围引用变量,则会遇到错误。