简体   繁体   English

将参数传递给setTimeout

[英]Passing a parameter into setTimeout

What is the deal with passing a parameter from a function into a setTimeout call? 将参数从函数传递到setTimeout调用有什么setTimeout Why is path here returning undefined ? 为什么这里的path undefined And what should I do instead? 我该怎么做呢?

$('.curatorSpace').bind('click', function() {
    var path = $(this).attr('data-path');
    setTimeout(function(path) {
        if($('#curatorRibbon').hasClass('ui-draggable-dragging')){return false}
        runOverlay(path);
    }, 100);
});

You don't need/must pass anything in there. 你不需要/必须传递任何东西。 path is a free variable and closured by the anonymous function you pass into setTimeout . path是一个自由变量,并通过传递给setTimeout的匿名函数setTimeout Hence, you can just access it. 因此,您可以访问它。

setTimeout(function() {
    if($('curatorRibbon').hasClass('ui-draggable-dragging')){return false}
    runOverlay(path);  // path gets resolved in the parent context
}, 100);

actually, by declaring path as formal parameter of that anonymous function, you've overwritten that variable lookup process through the scope chain. 实际上,通过将path声明为该匿名函数的形式参数,您已经通过作用域链覆盖了该变量查找过程。 Just get rid of that. 摆脱它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM