474,037 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP and settimeout scope

Hi,

I've been searching google and the archives here but haven't found a
clear solution. I want to do something like the following

function C(){
this.message = 'hi';
}
C.prototype.set = function(){
setTimeout(this .talk, 4000);
};
C.prototype.tal k = function(){
alert(this.mess age);
};

This doesn't do what I want, of course. I understand that when the
"talk" method runs the "this" won't be the same "this" as when the
"set" method runs. I would like the two "this" references to mean the
same instance of C and not the window. Is there a simple solution?

If closures are the solution is there a way to avoid moving the method
definititions into the constructor? That seems like cluttering the
constructor to me. But if that is the simplest it is ok too.

Thanks,
Peter

Jun 14 '06 #1
2 9462


pe**********@gm ail.com wrote:
function C(){
this.message = 'hi';
}
C.prototype.set = function(){
setTimeout(this .talk, 4000);
};


A closure can help
C.prototype.set = function () {
var thisC = this;
setTimeout(func tion () { thisC.talk(); }, 4000);
};
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 14 '06 #2

Martin Honnen wrote:
pe**********@gm ail.com wrote:
function C(){
this.message = 'hi';
}
C.prototype.set = function(){
setTimeout(this .talk, 4000);
};


A closure can help
C.prototype.set = function () {
var thisC = this;
setTimeout(func tion () { thisC.talk(); }, 4000);
};


Martin,

Thanks. This is a lot simpler than what I found in google. Just what I
was looking for.

(Totally unrelated but I just discovered that the google groups reply
button now automatically quotes.)

Peter

Jun 14 '06 #3

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

Similar topics

3
15002
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
7
1656
by: Antony Sequeira | last post by:
Hi While looking at some code I realized that the built in setTimeout function takes a string that is later evaluated in the original caller's context. How does one achieve something similar in user defined functions. -Antony
3
4498
by: Dica | last post by:
this works: document.all.style.visibility = 'hidden'; this doesn't setTimeout("document.all.style.visibility = 'hidden'",1000); looks like sResponseDivID isn't recognized. what's the problem? tks
6
2266
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id = document.getElementById(divId); setTimeout(id.style.display = 'none',timeout); } Thanks for any help.
12
5577
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
1
1733
by: Joris Lambrecht | last post by:
Hi people, Please take a look at the issue i talk about below. (yes i do realise such functions are publicaly available) The page i'm using uses JSON defined array of image files with information for the title property of the img tag. The functions i wrote below are sort of working since it needs an alert() to work the slides ???
15
3817
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
7
6283
by: -Lost | last post by:
I am calling setTimeout within the context of an object, and whilst this exists, it refuses to be passed along to the function I call. For example: $elemById('id').change = function() { // the function returns the object itself, 'this' exists alert(this); // setTimeout('alert(this);',1000); // }
4
5215
by: Adam Risser | last post by:
Hi everyone, I am modifying the suckerfish dropdown code to use settimeout to have a slight pause before the menus disappear to make it more user friendly. I have hit a snag with the following statement: li.onmouseout=function() { timerID=setTimeout('this.getElementsByTagName("UL") .style.display = "none"', timecount);
7
2427
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to interfer with one another.
0
10532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10328
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12120
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11591
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4933
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3951
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.