javascript - Return value from deferred nested function -
i'm writing caching function loads invisible content dom div sizes can calculated correctly (including images). using jquery's deferred object run function once caching complete.
the problem having can't work out how return props object once caching function complete. return props
@ bottom want return properties object, it's returning undefined _obj function hasn't completed time return called.
my complete function near bottom correctly logging props object (inc cacheheight var), can't work out how return props object deferred function. i'd return _obj(content).done(complete);
, returns deferred object, not return complete function.
cache : function(content) { // vars var props; // deferred switch var r = $.deferred(); /* * cache object */ var _obj = function(content) { cacheheight = 0; cache = document.createelement("div"); cache.style.visibility = "hidden"; $(cache).css("position", "fixed"); // prevents parent's size being affected $(cache).append(content); $(contentwrapper).append(cache); children = $(cache).children(); // image loader var imgs = $(cache).find('img'), img_length = imgs.length, img_load_cntr = 0; if (img_length) { //if $img_container contains new images. imgs.on('load', function() { //then avoid callback until images loaded img_load_cntr++; if (img_load_cntr == img_length) { remaining = children.length; $.each(children, function(index, value) { --remaining; cacheheight = cacheheight + parseint($(value).outerheight(false)); if (remaining == 0) { props = { height : cacheheight } r.resolve(); } }); } }); } return r; }; /* * return cached object data */ var complete = function () { console.log(props); // correctly logs props object }; // resolve when finished _obj(content).done(complete); console.log(props); // logs props undefined (not good) // return?!?! return props; }
what if passed in function callback cache parameters access props variable. i'm wondering if returning props before it's set.
Comments
Post a Comment