cod*_*ove 3 javascript jquery settimeout
将参数从函数传递给setTimeout调用的处理是什么?为什么path回来undefined?我该怎么做呢?
$('.curatorSpace').bind('click', function() {
var path = $(this).attr('data-path');
setTimeout(function(path) {
if($('#curatorRibbon').hasClass('ui-draggable-dragging')){return false}
runOverlay(path);
}, 100);
});
Run Code Online (Sandbox Code Playgroud)
你不需要/必须传递任何东西.path是一个自由变量,并由您传入的匿名函数关闭setTimeout.因此,您可以访问它.
setTimeout(function() {
if($('curatorRibbon').hasClass('ui-draggable-dragging')){return false}
runOverlay(path); // path gets resolved in the parent context
}, 100);
Run Code Online (Sandbox Code Playgroud)
实际上,通过声明path该匿名函数的形式参数,您已经通过作用域链覆盖了该变量查找过程.摆脱它.
| 归档时间: |
|
| 查看次数: |
802 次 |
| 最近记录: |