By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,970 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,970 IT Pros & Developers. It's quick & easy.

Way of halting execution until certain event completes

Mike Kypriotis
P: 37
I have a loop where Itest if each of a set of images exists and if so display it the function for it is
Expand|Select|Wrap|Line Numbers
  1. function checkImgs()
  2. {
  3.     if (document.images)
  4.     {    
  5.           var imgsrc = 'Lo.jpg';
  6.           var img = new Image();
  7.  
  8.         img.onerror = function () {
  9.           alert(this.width + ',' + this.height + " can't be loaded.");
  10.           }
  11.         img.onload = function () {
  12.           alert(this.width + ',' + this.height + " is loaded.");
  13.           }
  14.  
  15.         img.src = imgsrc;
  16.     }
  17. }
  18.  
the plan is to set a variable inside the onload event and after calling checkImgs() to simply check that variable, problem is that program execution will not halt so almost always the script will proceed to checking the variable before the onload event fires and modifies it. And I cannot put my entire code to the onload event.Thought about putting the variable checking into a while where it won't exit while the variable is 0 (the events will either give it say a value of 1 or 2)but it is a good practice?
May 9 '11 #1
Share this Question
Share on Google+
1 Reply


Mike Kypriotis
P: 37
decided to put everything in those two events... another question is is it 100% sure that one of the two events will be fired, cause if not my script will come to an unexpected halt (omitting the onabort which is a user action and thus if the user wants to stop loading/execution of the page nothing will happen)
May 10 '11 #2

Post your reply

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