jquery - Passing/Accessing values from separate thread in javascript -


i trying use following code had found/edited in post. use function "isvalidimageurl()" instantly return result on fly. since function runs in separate thread, had trouble getting value, example, if had "return true;" in place of "isvalid=true;" "undefined" in return. tried use global variable "isvalid", seem 1 step behind, meaning need call function twice, result first call.

what fundamentally missing? or able suggest method can it?

thank you!

<script>     var isvalid;     function isvalidimageurl(url) {         $("<img>", {                       src: url,                       error: function() { isvalid=false; },                       load: function() { isvalid=true; }                     });      }      isvalidimageurl("https://www.google.com/logos/2012/hertz-2011-hp.gif");     alert(isvalid);     isvalidimageurl("http://google.com");     alert(isvalid); </script> 

the problem have wait image either load or fail before can use status. best way give isvalidimgurl function callback function (or seperate ones success , failure) run when image has loaded or error has occured.
in example image element passed callback functions, know image called for.

function isvalidimageurl(url, okfunc, errfunc)  {      var image = document.createelement("img");      image.src = url;      image.onload = function() {okfunc(image)};      image.onerror = function() {errfunc(image)};  }    function imageloadsucces(image)  {      alert("image loaded: " + image.src);  }    function imageloadfail(image)  {      alert("image load error: " + image.src);  }    isvalidimageurl("https://www.google.com/logos/2012/hertz-2011-hp.gif", imageloadsucces, imageloadfail);  isvalidimageurl("http://google.com", imageloadsucces, imageloadfail);


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -