javascript - JS Asynchronous calls - my callback method is not working to check an image dimensions -


i have boolean function here called checkurl checks if picture has 100 x 100 pixels. if returns true. there asynchronous call within function check size of image.

function checkurl(url, callback) {      var valid;     var img = new image();     img.src = url;      img.onload = function (callback) {          valid = callback(img.width, img.height);      };     console.log(valid);     return valid; }   function nextstep(w, h) {     if (w == 100 && h == 100) {         return true;     } else {         return false;     } }  var pic_valid = checkurl(a_url, nextstep); 

i getting error:

callback not defined

within checkurl function. worrying variable valid within checkurl end being invalid.

additional edit: if add console.log(valid); before returning valid undefined. expected?

you define callback here:

function checkurl(url, callback) { 

then redefine here:

img.onload = function(callback){ 

… masks variable give value to.

don't mask it:

img.onload = function(){ 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -