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 Ajax 2nd Edition ISBN: 978-0-470-10949-6
This is the forum to discuss the Wrox book Professional Ajax, 2nd Edition by Nicholas C. Zakas, Jeremy McPeak, Joe Fawcett; ISBN: 9780470109496
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Ajax 2nd Edition ISBN: 978-0-470-10949-6 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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 1st, 2009, 04:16 AM
Registered User
 
Join Date: Nov 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default parameters in event handlers

In the 2nd to last page of your book you say:

"By defining these functions globally, you avoid using closures to assign event handlers. Closures are a manner in which it's possible to define a function that makes use of variables defined outside of it. They also happen to be the main cause of memory leaks in many web browsers. Whenever possible, it is preferable to create standalone functions to use as event handlers."




Does that mean that I shouldn't pass parameters to event handlers? Does that mean that I shouldn't do this:


function getInfoFromServer(numTries)
{
//more code not shown here
mTimer = setTimeout('getInfoFromServer(' + numTries + ')', iIntervalTryAgain);
}

This is for a periodic refresh. If an error occurs I want to try again. But I don't want to keep trying forever, so I pass the numTries so that I can keep track of how many tries I've made.

Is it okay to do it this way (I'm passing parameters to settimeout())? If not, how can I do this?


Here is the context in which the settimeout is called:


numTries++;
receiveReq.open("post", "[pagename]", true);
receiveReq.setRequestHeader("connection", "close");
receiveReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

receiveReq.onreadystatechange = function () {
if (receiveReq.readyState == 4) {
if (receiveReq.status == 200) {
handleResponse(receiveReq.responseText);
mTimer = setTimeout('getInfoFromServer(0)', iInterval);
} else if(receiveReq.status == 12030 || receiveReq.status == 12031) {
alert('error occurred but Im continuting');
clearInterval(mTimer);
mTimer = setTimeout('getInfoFromServer(' + numTries + ')', iIntervalTryAgain);
} else {
alert("An error occurred: " + receiveReq.statusText + 'status code: ' + receiveReq.status);
clearInterval(mTimer);
mTimer = setTimeout('getInfoFromServer(' + numTries + ')', iIntervalTryAgain);
}
}
};


p.s. Am I handling the 12030 status code correctly?

Last edited by ajaxbeginner; December 1st, 2009 at 04:22 AM.
Reply With Quote
  #2 (permalink)  
Old December 3rd, 2009, 10: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

That is fine. A closure looks like this:

Code:
var value;
function doSomething(){
    alert(value)
}
It doesn't have anything to do with parameter passing. Your onreadystatechange handler is a closure because it uses receiveReq, which is defined outside of it.

Your code should be fine. :)
__________________
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
Reply


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
Event handlers jwebb Visual Basic 2005 Basics 1 June 25th, 2007 08:44 PM
ie7 keycode, event handlers erobb Javascript 0 November 24th, 2006 12:37 PM
Event handlers does not work after some changes geetageetageeta ASP.NET 2.0 Basics 0 March 6th, 2006 03:19 AM
Conflicting Event Handlers chp Excel VBA 3 February 24th, 2006 02:00 PM
Dynamically change event handlers oranginalab Javascript How-To 2 July 31st, 2003 10:11 AM



All times are GMT -4. The time now is 03:18 PM.


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