Wrox Programmer Forums
| 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 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
  #1 (permalink)  
Old December 1st, 2009, 05: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 05:22 AM..
  #2 (permalink)  
Old December 3rd, 2009, 11: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/


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 01:37 PM
Event handlers does not work after some changes geetageetageeta ASP.NET 2.0 Basics 0 March 6th, 2006 04:19 AM
Conflicting Event Handlers chp Excel VBA 3 February 24th, 2006 03:00 PM
Dynamically change event handlers oranginalab Javascript How-To 2 July 31st, 2003 10:11 AM





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