Reputation: 25928
In Javascript is it possible to pass a member function into the function setInterval(). Hope this makes sense, I'll show you a code example of what I want to do because it's easier to explain that way.
I want to call the following function every 10 milliseconds & be able to access & alter the class member this.myArray() within that function(every time the function is called).
function myClass()
{
this.myArray = new Array()
setInterval(this.slideLoop, 10);
}
// THE WHOLE POINT OF ALL THIS IS SO I CAN ACCESS THE ARRAY this.myArray()
// INSIDE THE FOLLOWING FUNCTION WHEN ITS CALLED FROM setInterval() EVERY 10ms
myClass.prototype.slideLoop = function()
{
alert( this.myArray[0] );
this.myArray.slice(0,1);
}
Upvotes: 1
Views: 1621
Reputation: 4448
You can use a closure with something like
function myClass()
{
this.myArray = new Array()
var that = this;
setInterval(function() { that.slideLoop() }, 10);
}
For an example, see: http://jsfiddle.net/3gyXF/
(For the example I changed the timeout to 1s and the slice
to splice
to illustrate)
Upvotes: 5