.call or .apply with setTimeout

This is a discussion on ".call or .apply with setTimeout" within the JavaScript Forum section. This forum, and the thread ".call or .apply with setTimeout are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 9th, 2008, 13:00
Junior Member
Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
.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: Select all
<!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>
Reply With Quote

  #2 (permalink)  
Old Feb 9th, 2008, 13:27
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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: Select all
setTimeout('hello.call(this.seti, [20])', 1000);
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Feb 9th, 2008, 18:00
Junior Member
Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Reply With Quote
  #4 (permalink)  
Old Feb 10th, 2008, 04:09
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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: Select all
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');
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Feb 10th, 2008, 13:33
Junior Member
Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
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; Feb 10th, 2008 at 13:38.
Reply With Quote
  #6 (permalink)  
Old Feb 29th, 2008, 16:41
New Member
Join Date: Feb 2008
Location: Boulder
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Reply With Quote
Reply

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 Forum 1 Apr 22nd, 2008 16:30
Apply transition to a div blkskull JavaScript Forum 0 Feb 26th, 2008 14:00
setTimeout won't work cheataweb JavaScript Forum 1 Dec 27th, 2006 00:26
Beginning to Apply my skillz... courtjester Classic ASP 7 Apr 5th, 2004 10:18


All times are GMT. The time now is 06:03.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43