Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 2nd Edition by Nicholas C Zakas; ISBN: 9780470227800
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 30th, 2009, 08:19 PM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 1
Thanked 1 Time in 1 Post
Default Passing parameters to timers

Hi,

I just wanted to compliment Nicholas C. Zakas to his excellent book on JavaScript. I have just bought the 2nd Edition and I enjoy reading it. But still after reading and experimenting - especially the advanced technique chapter (18) - here is a question about timers. I haven't found yet a good working solution how to pass parameters to a timer.

Example source:

{...}
setTimeout(function(parmeter1, parameter2,...) {...}, 10);
{...}

Do you have maybe a practical solution to this issue? Have ever come across such problem?

Yours
tMesi
  #2 (permalink)  
Old January 31st, 2009, 03:59 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default Re: Passing parameters to timers

Hi,

I think what you're looking for is not the ability to pass parameters, but rather the ability to bind parameters to a function that you've passed into setTimeout(). To do that, you can use either the curry() function described in the same chapter or the last version of the bind() function:

Code:
var param1 = 2;
var param2 = 4;

function functionToPass(arg1, arg2){

}

setTimeout(bind(functionToPass, this, param1, param2), 10);
This ensures that param1 and param2 are bound to functionToPass() when the timer is executed. Hope this helps.

-Nicholas
__________________
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/

Last edited by nzakas; January 31st, 2009 at 04:00 PM. Reason: Improper formatting.
The Following User Says Thank You to nzakas For This Useful Post:
tMesi (February 1st, 2009)
  #3 (permalink)  
Old March 21st, 2009, 07:09 PM
Registered User
 
Join Date: Dec 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by nzakas View Post
Code:
var param1 = 2;
var param2 = 4;

function functionToPass(arg1, arg2){

}

setTimeout(bind(functionToPass, this, param1, param2), 10);
This ensures that param1 and param2 are bound to functionToPass() when the timer is executed.
Just wanted to note that you must be referring to the bind() function found on page 597, rather than the more simple version found on page 595. This revelation comes after spending several minutes trying to figure out how the one on page 595 could possibly work for this issue.

(It may be useful to add page 597 to the index ("595, 597") to alert readers to the second version of the function.)
 


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing parameters fizzerchris Access VBA 5 August 31st, 2007 12:16 PM
Passing parameters tal Ajax 5 May 26th, 2007 08:07 AM
Passing parameters ceema Crystal Reports 0 February 15th, 2006 03:03 AM
Passing Parameters spraveens Javascript 5 October 8th, 2003 07:06 AM
passing parameters spraveens Javascript How-To 1 October 8th, 2003 03:14 AM



All times are GMT -4. The time now is 08:50 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.