-3

What I observe is that the value only changes during the execution of the function and then returns to the original value, or else this execution (onload) is made after reading the global variable. I need only one statement: How do I test if an image (jpg) was loaded successfully and then use that value shortly after the check. I have tried everything (two days), until I used dynamic form element creation during the function execution (input value), and even then the value of the inputs DO NOT CHANGE to be read after the function. Oh, and before lowering my reputation again, consider that I do not speak English and live in the underdeveloped belt of the globe.

var imagem = '0';
var myImg = new Image();
myImg.src = 'Alfabeto-0002.jpg';
myImg.onload = function(){
      document.write('Image Loaded');
      imagem='1';
}
myImg.onerror = function(){
      document.write('Image NOT Loaded');
      imagem='2';
}
alert(imagem);
  • It is changed once the function is called. The `alert()` *will* be called before `imagem`s value has been changed – Luca Kiebel Apr 07 '18 at 17:30
  • The value of the variable changes only within the function, so that if I use document.write () or console.log () instead of alert (), the result is the same: the value of the variable DOES NOT CHANGE. All I needed was for someone to help me find a way to solve this problem. I need the function to change the value PERMANTENTLY, not just during function execution. I did not need the moderators to lower my reputation and mark the question as repeated, NOT REPEATED, are WELL different situations. If I knew what they knew, I would not even be here to learn. – Eibo - Sistemas Web Apr 08 '18 at 17:58
  • The linked question has the exactly same problem that you are having – Luca Kiebel Apr 08 '18 at 18:11
  • I read all that and, as I understand it, there is no way to do what I need. Okay, let's say it's a javascript limitation. – Eibo - Sistemas Web Apr 08 '18 at 18:14
  • It's a limitation of asynchronous functions, or better, it's what is good about asynchronously handling stuff in JS – Luca Kiebel Apr 08 '18 at 18:16
  • In PHP it is so easy, I simply test if the file exists (file_exists), I found that javascript was also capable of this in an easy way. – Eibo - Sistemas Web Apr 08 '18 at 18:16
  • I was trying to create a page that would read images in a directory and catalog them automatically. The photos would be in numerical order: foto1, foto2, foto3. New photos would be added respecting the numerical order and I would have a dynamic catalog. So I needed to test if the photo was loaded, because when I got to the last photo, I should stop reading. That's all I needed. – Eibo - Sistemas Web Apr 08 '18 at 18:19

1 Answers1

1

It does change the variable. Your alert() call is just made before the image load event. Put the alert() inside the onload/onerror functions and you will see that the value changed.

Máté Safranka
  • 4,081
  • 1
  • 10
  • 22
  • The value of the variable changes only within the function, so that if I use document.write () or console.log () instead of alert (), the result is the same: the value of the variable DOES NOT CHANGE. All I needed was for someone to help me find a way to solve this problem. I need the function to change the value PERMANTENTLY, not just during function execution. I did not need the moderators to lower my reputation and mark the question as repeated, NOT REPEATED, are WELL different situations. If I knew what they knew, I would not even be here to learn. – Eibo - Sistemas Web Apr 08 '18 at 17:58