473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

'this' and setInterval

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
Nov 23 '05 #1
3 2345

Andrew Poulos wrote:
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

Nov 23 '05 #2
VK

Andrew Poulos wrote:
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();

Nov 23 '05 #3
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 ;-)

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Richard A. DeVenezia | last post by:
At line this.timerId = setInterval (function(){this.step()}, 100) I get error dialog Error:Object doesn't support this property or method. If I change it to this.timerId = setInterval...
5
by: Richard A. DeVenezia | last post by:
Is some future Mozilla going to support setInterval ( <function:function>, <interval:number> ) ? Right now it seems to be simply setInterval ( <function-text:string>, <interval:number> ) --...
1
by: Weston C | last post by:
In the course of trying to build a simple clock, I've run into a problem using the setInterval (and setTimeout) function. http://weston.canncentral.org/misc/tkeep/tkeep.html...
3
by: Eli | last post by:
Hello all, I have just added a small piece of JavaScript code to a basic HTML page of mine. At the end of the page (after the closing </HTML>) I added: <SCRIPT LANGUAGE="JavaScript">...
4
by: barry | last post by:
If The script below is exeuted from the onload="startit()" in the body tag everything works fine but when I remove the onload and try to execute by using the attributes.add from within the Page_Load...
3
by: shawn | last post by:
Hi All Was trying to get this bit of code working (simplified of course) for (i=0;i<uuid_num;i++) { window.setInterval(InitMap(uuidValues), timePeriod); } Where uuid_num, uuidValues,...
2
by: shiznit | last post by:
checkWin = function(x) { alert(x); // Displays undefined } function startScroll(e) { if ( TimeOutID == null)
4
by: RgeeK | last post by:
From my newbie perspective I understand that Javascript is a single threaded environment and so interrupt-driven events are going to be somewhat challenging. But, I have an issue which seems to...
1
by: jonahmason | last post by:
hi. a javascript newbie here in dire need of help. for the past 3 days i've been trying to get a script using setinterval to work but a part of the code, that executes after setinterval hits a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.