I’ve been trying to figure this out for a while but now thought I would just post it here and see if I can finally understand this issue with setInterval I am having.
In case this matters, I’m using jQuery 1.4.4 in this document.
Given the following:
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);
}
}
This however results in:
go is not defined
Which is ‘correctly’ being logged every 1200 ms. So how can I run my function go() including passing in the values for slideNumber, direction?
Try this:
Can’t tell from what you posted, but I’m guessing that that code is all inside some function somewhere. The “go” function would have to be global for that to work. When you pass just a string, the interpreter evaluates that in the global context when the timer fires. By using a real function like in the example I provided, you capture that local “go” in a closure.