2

我一直试图解决这个问题,但现在我想我会把它贴在这里,看看我是否最终能理解setInterval我遇到的这个问题。

如果这很重要,我在本文档中使用 jQuery 1.4.4。

鉴于以下情况:

var MS = {},
    MS.timer = 1200; // this both would be user accessible by the plugin

// if the timer option is set then activate slideshow
if ( MS.timer ) { setInterval( "go(2,'right')" , MS.timer); }

// show the slide (as defined by the pass ins)
function go(slideNumber, direction) {
    if ( !paused || !running ) { 
        console.log('run!'+', '+slideNumber+' , '+direction);
    }
}

然而,这会导致:

go is not defined

每 1200 毫秒“正确”记录一次。那么如何运行我的函数go(),包括传入的值slideNumber, direction

4

3 回答 3

3

试试这个:

if ( MS.timer ) { setInterval( function() { go(2,'right'); }, MS.timer); }

无法从您发布的内容中看出,但我猜该代码都在某个函数中的某个地方。“go”函数必须是全局的才能工作。当您只传递一个字符串时,解释器会在计时器触发时在全局上下文中评估它。通过使用我提供的示例中的真实函数,您可以在闭包中捕获本地“go”。

于 2010-11-16T20:52:52.110 回答
0

尝试:

setInterval(function() {
   go(2, 'right');
}, MS.timer);
于 2010-11-16T20:53:41.920 回答
0

试试这个:

window.setInterval(go, MS.timer, [2, 'right']);
于 2010-11-16T20:54:02.920 回答