-2

I want to trigger a new function once this function returns a callback. That is displayed here:Create a custom callback in JavaScript How do I do that?

function loadImg (imgSrc, callback) {
    var newImg = new Image();
    newImg.onload = function() {
        var height = newImg.height;
        var width = newImg.width;
        if(height > width){
            console.log(false);
            if(callback) callback(false);
        } else {
            console.log(true);
            if(callback) callback(true);
        }
     }
     newImg.onerror = function () {
         if(callback) callback('error');
     }
     newImg.src = imgSrc;
}
Community
  • 1
  • 1
Himmators
  • 14,278
  • 36
  • 132
  • 223
  • 1
    please refer... http://stackoverflow.com/questions/2190850/create-a-custom-callback-in-javascript – Amit Sharma May 16 '13 at 12:43
  • I'm not sure what you mean with *"once this function returns a callback"*. You can call the other function inside the callback or after you called the callback, depending how the code is related. – Felix Kling May 16 '13 at 12:43
  • That function won't *return* a callback, it has no `return` statement. – Quentin May 16 '13 at 12:44
  • @FelixKling Hrm, okay, then I need to know how to pass $('this') as a parameter to the function. – Himmators May 16 '13 at 12:45
  • `callback(true, $('this'));` (for some value of "the function", since you have at least 6 that I can count in the question) – Quentin May 16 '13 at 12:45
  • Use this library to detect image onload events: https://github.com/desandro/imagesloaded - It will also work if images are cached. – jantimon May 16 '13 at 12:46
  • @Quentin I'm new to asyncronous stuff. How do I return the callback once the newImg has loaded? – Himmators May 16 '13 at 12:46
  • You are already calling the callback when you call the callback in the `if` statement that checks for a `callback` that has been passed to the `loadImg` `callback` parameter. – SomeShinyObject May 16 '13 at 12:46
  • You don't *return* callbacks. You *call* callbacks. – Felix Kling May 16 '13 at 12:46
  • @KristofferNolgren — Why would you want to return anything? Nothing is going to pay attention to the return value of a load event handler. – Quentin May 16 '13 at 12:47
  • Like you're already doing: `if(callback) callback(false);`!!! (Yes, I think a triple exclamation mark is justified). – Quentin May 16 '13 at 12:47
  • I think it's better if you explain the problem you are actually trying to solve. What are you trying to achieve with all of this? – Felix Kling May 16 '13 at 12:49

1 Answers1

0

As has already been stated, you are already calling your callback when you check for it...and then call it:

if(callback) callback(false);

Run this code (jsFiddle linked at bottom). It's the same concept as your code.

function doMe(something, callback) {
    console.log("Congrats, I did " + something);  
    if(callback) {
       callback();   
    }
}

doMe("the disco", function() {
   console.log("I'm a callback"); 
});
//I'm a callback

jsFiddle

Alternatively, you can make it a little more "literal" and use the call() method to pass your arguments:

function doMe(something, callback) {
    console.log("Congrats, I did " + something);  
    if(callback) {
       callback.call(this, true);   
    }
}

doMe("the disco", function(bool) {
   console.log("I'm a callback. " + bool + " story"); 
});
// I'm a callback. true story

another fiddle

SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59