'this' and setInterval

A

Andrew Poulos

If I have some code that's a bit like this

Con = function() {
this.op = 1;
this.count = 0;
}
Con.prototype.loop = function() {
this.doNext = setInterval(this.next, 100);
}
Con.prototype.next = function() {
this.count++;
if (this.count > 10) clearInterval(this.doNext);
}

obj = new Con();

when I call obj.loop(); 'this' in obj.next refers to setInterval
and not obj . I realise I can add var ref = this; and send it with
setInterval but is there something I can do to get 'this' to refer to
obj when in obj.next ?


Andrew Poulos
 
J

Julian Turner

Andrew said:
If I have some code that's a bit like this

Con = function() {
this.op = 1;
this.count = 0;
}
Con.prototype.loop = function() {
this.doNext = setInterval(this.next, 100);
}
Con.prototype.next = function() {
this.count++;
if (this.count > 10) clearInterval(this.doNext);
}

obj = new Con();

when I call obj.loop(); 'this' in obj.next refers to setInterval
and not obj . I realise I can add var ref = this; and send it with
setInterval but is there something I can do to get 'this' to refer to
obj when in obj.next ?

AFAIK, the contents of the "this" keyword cannot be altered, and
closures are a normal way of handing the problem:-

Con.prototype.loop = function() {
var ref=this;
this.doNext = setInterval(function(){ref.next();}, 100);
}

Regards

Julian
 
V

VK

Andrew said:
If I have some code that's a bit like this

Con = function() {
this.op = 1;
this.count = 0;
}
Con.prototype.loop = function() {
this.doNext = setInterval(this.next, 100);
}
Con.prototype.next = function() {
this.count++;
if (this.count > 10) clearInterval(this.doNext);
}

obj = new Con();

when I call obj.loop(); 'this' in obj.next refers to setInterval
and not obj . I realise I can add var ref = this; and send it with
setInterval but is there something I can do to get 'this' to refer to
obj when in obj.next ?

Besides closures you can also overload your function. I'm not saying
it's anyhow better than closures, just another way:

function Con() {

this.counter = 0;
this.timerID = null;

this.moveNext = function() {
with (arguments.callee.context) {
counter++;
if (counter > 10) {
clearInterval(timerID);
alert('Show is over');
}
}
}

this.startLoop = function() {
this.moveNext.context = this;
this.timerID = self.setInterval(this.moveNext, 100);
}
}


obj = new Con();
obj.startLoop();
 
L

Luke Matuszewski

This is quite confusing... we all know that every object created via
'new' has a prototype object shared with all created objects with the
same constructor (assuming that prototype was defined before and not
changed during creation of new objects), so when you do:
Con.prototype.next = function() {
this.count++;
if (this.count > 10) clearInterval(this.doNext);
}
and call it via obj.next(); then 'this' should point to obj
itself...(when you call Con.prototype.next() then 'this' references to
only prototype object.
It is wise to do at begginging:
Con = function() {
var thisRef = this;
....
}

Maybe someelse would post another comments ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top