My code looks like this:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
return false;
};
image.onload = function() {
return true;
};
image.src = '.jpg';
});
But, it doesn't work, assumedly because my returns only return from the anonymous function, not from the one called imageCheck. How can I rewrite this so that the whole function is returned as true or false?
My code looks like this:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
return false;
};
image.onload = function() {
return true;
};
image.src = 'http://www.example./image.jpg';
});
But, it doesn't work, assumedly because my returns only return from the anonymous function, not from the one called imageCheck. How can I rewrite this so that the whole function is returned as true or false?
Share Improve this question asked Apr 27, 2011 at 19:13 Rich BradshawRich Bradshaw 73.1k46 gold badges188 silver badges241 bronze badges 1- What is myMethod and what is imageCheck? Why are you returning something in a handler function for an async call? That return data does not do anything. You have to use the "continuation" style of programming for async. – Stephen Chung Commented Apr 28, 2011 at 9:53
1 Answer
Reset to default 6you have to use callbacks, for example:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
returnCallback(false);
};
image.onload = function() {
returnCallback(true);
};
image.src = 'http://www.example./image.jpg';
});
function returnCallback(bool){
//do something with bool
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745655020a4638492.html
评论列表(0条)