473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

assigning a new object to a variable deletes the previous and all its event bindings?

Mike Kypriotis
I have loop where at each loop I need a new image object so if I put a
Expand|Select|Wrap|Line Numbers
  1. var img=new Image();
  2. addEvent(img,'load',function(){validBannerImage(iNum,oInputData);});
  3. addEvent(img,'error',function(){invalidBannerImage(iNum,oInputData);});
  4.  
at each loop when I assign the new object the previous get deleted as well as its bindings to functions (for the onload and onerror events)

P.S Tried to define the above before the loop but needed the parameters (iNum,oInputData) to call the fuctions and they are calculated inside the loop
Dec 29 '11 #1
5 1479
Dormilich
8,658 Expert Mod 8TB
P.S Tried to define the above before the loop but needed the parameters (iNum,oInputData) to call the fuctions and they are calculated inside the loop
that’s the problem. you are trapped by a Closure. you will therefore get the values of the parameters in their last loop cycle.

to counter that, break the Closure through a "Counter-Closure"
Expand|Select|Wrap|Line Numbers
  1. for (…) {
  2.     var param1 = …, param2 = …;
  3.     (function (p1, p2) {
  4.         addEvent(img, 'load', function() {
  5.             validBannerImage(p1, p2);
  6.         });
  7.     })(param1, param2);
  8. }
Dec 30 '11 #2
hmmm...didn't quite understood you. Think is I need an image objeect in each loop so Imove my 3 statements
Expand|Select|Wrap|Line Numbers
  1. var img=new Image();
  2. addEvent(img,'load',function(){validBannerImage(iNum,oInputData);});
  3. addEvent(img,'error',function(){invalidBannerImage(iNum,oInputData);});
  4.  
inside the loop and all is ok thing is when i redifine the img as new image() the old and its event handlers (which are no longer in use + I do not use them anymore) get automatically deleted ? cause the loop runs forever (its an image scroller) so the amount of useless image objects will become a problem at some time if they are not deleted
Dec 31 '11 #3
Dormilich
8,658 Expert Mod 8TB
cause the loop runs forever
you mean it’s an infinite loop?
Jan 1 '12 #4
yes its is an infinite loop so "assigning a new object to a variable deletes the previous and all its event bindings?" would be very helpful if the answer was yes (I reassign a new object to the same variable in each loop )
Jan 1 '12 #5
Dormilich
8,658 Expert Mod 8TB
yes its is an infinite loop
that is a no-go in JavaScript, because it is event-driven

"assigning a new object to a variable deletes the previous and all its event bindings?"
let’s put it this way: if you delete the last reference to an object (objects are always passed by reference) JS deletes the object with all its associated data. assigning a new object to a variable holding a reference to another object will delete that previous reference.
Jan 1 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Joe Blow via DotNetMonster.com | last post by:
Hello, I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm...
3
by: datactrl | last post by:
Hi, all Is that possible I can assign an window object variable to an already opened window? With window.open(), we can get a window object from opened window. If a window has already opened, is...
6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
4
by: nelsonchang | last post by:
Dear all, I wrote the VB code in a "button press event" as below in which is retrieve data from SQL server and insert the data into Excel. After that, I would call Excel "subtotal" API to build a...
2
by: Ivan Sammut | last post by:
Hi, I have a form which contains about 100 Textboxes but I mapped all their onTextchanged event to the same procedure. Now I would like to know if it is possible from the sender argument passed...
3
by: Richard Hollenbeck | last post by:
I've marked the line in this subroutine where I've been getting this error. It may be something stupid but I've been staring at this error trying to fix it for over an hour. I'm pretty sure the...
9
by: axs221 | last post by:
I am trying to move some of our large VBA Access front-end file into ActiveX DLL files. I created two DLL files so far, one was a module that contains code to integrate into the QuickBooks...
3
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
0
by: David C | last post by:
I am getting this error in the Databound event of a GridView and I am having a difficult time debugging. I do not get the error on every record that I edit in the GridView. The error is occurring...
0
by: Paul Morriss | last post by:
I get this error from an application: ************** Exception Text ************** System.NullReferenceException: Object variable or With block variable not set. at...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.