Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2009, 09: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
 
Old January 31st, 2009, 04:59 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
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 05:00 PM.. Reason: Improper formatting.
The Following User Says Thank You to nzakas For This Useful Post:
tMesi (February 1st, 2009)
 
Old March 21st, 2009, 07:09 PM
Registered User
 
Join Date: Dec 2005
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.)





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 04: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.