iEntry 10th Anniversary Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > JavaScript

Reply
 
LinkBack Thread Tools
Old February 9th, 2008, 10:00 AM   #1
New Member
 

Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 djeyewater is on a distinguished road
.call or .apply with setTimeout

How do I get the below timeout to work? After reading http://www.west-wind.com/WebLog/posts/5033.aspx I think I need to use .call or .apply within the timeout, but I can't figure out how to use them?

HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
function hello()
{
    this.i=0;
    this.bye();
    
    alert(this.i);
}
hello.prototype.bye = function()
    {this.i++;}
hello.prototype.timeOut = function()
{setTimeout(function(){hello.call(this.seti, [20]);}, 1000);}
hello.prototype.seti = function(l)
{
    this.l=l;
    this.i=l;
    this.bye();
}
</script>
</head>

<body onload="jim = new hello()">
<a href="javascript: jim">jim</a><br>
<a href="javascript: jim.seti(10)">seti(10)</a><br>
<a href="javascript: alert(jim.i)">alert(jim.i)</a><br>
<a href="javascript: alert(jim.l)">alert(jim.l)</a><br>
<a href="javascript: jim.timeOut()">jim.timeOut()</a>
</body>
</html>
djeyewater is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 9th, 2008, 10:27 AM   #2
Highly Reputable Member
SuperMember
 
Rakuli's Avatar
 

Join Date: Sep 2007
Location: Australia
Age: 25
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold
Re: .call or .apply with setTimeout

The first argument of setTimeout is an evaluated string so you just need to place quotes around it.

Code:
setTimeout('hello.call(this.seti, [20])', 1000);
__________________
Luke Dingle . com
Rakuli is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 9th, 2008, 03:00 PM   #3
New Member
 

Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 djeyewater is on a distinguished road
Re: .call or .apply with setTimeout

Thanks for the pointer, but I still can't get it to work using that code. Also, I tried removing the reference to this.bye() from seti(), and still got the error from the timeout "this.bye is not a function", so I think its trying to create a new hello object but with 'this' pointing to the window object.
djeyewater is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 10th, 2008, 01:09 AM   #4
Highly Reputable Member
SuperMember
 
Rakuli's Avatar
 

Join Date: Sep 2007
Location: Australia
Age: 25
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold
Re: .call or .apply with setTimeout

That's right, you can't use the this keyword because it is not available in the scope from where you're calling it.

You can look at naming the obj so it can be aware within the function:

Code:
function hello(objName)
{
    this.i=0;
    this.Oname = objName;
    this.bye();
    
    alert(this.i);
}
hello.prototype.bye = function()
    {this.i++;}
hello.prototype.timeOut = function()
{setTimeout(this.Oname + '.this.seti()', [20]);}, 1000);}
hello.prototype.seti = function(l)
{
    this.l=l;
    this.i=l;
    this.bye();
}
Then when you create the object, pass the name of the variable to the obj.

var jim = new hello('jim');
__________________
Luke Dingle . com
Rakuli is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 10th, 2008, 10:33 AM   #5
New Member
 

Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 djeyewater is on a distinguished road
Re: .call or .apply with setTimeout

Thank you so much for that, works great!

Edit: Mark thread as solved isn't appearing in the Thread Tools for me, so if a Mod can mark this thread as solved, it would be appreciated.

Last edited by djeyewater; February 10th, 2008 at 10:38 AM..
djeyewater is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old February 29th, 2008, 01:41 PM   #6
New Member
 

Join Date: Feb 2008
Location: Boulder
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 NeilM is on a distinguished road
Re: .call or .apply with setTimeout

You can also accomplish this with a "closure" variable pointing to "this"

var self = this;
setTimeout(function() {self.seti(); }, 1000);

This way is much less limiting then working with strings. If you need to pass parameters to your function, look into the "hitch" method which uses call and apply and allows for arguments.
NeilM is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with setTimeout in method Emancip8 JavaScript 1 April 22nd, 2008 01:30 PM
Apply transition to a div blkskull JavaScript 0 February 26th, 2008 11:00 AM
setTimeout won't work cheataweb JavaScript 1 December 26th, 2006 09:26 PM
Beginning to Apply my skillz... courtjester Classic ASP 7 April 5th, 2004 07:18 AM


Search Engine Optimization by vBSEO 3.2.0 RC8