jquery - ajaxComplete, previously array empty -
// start <div id="ms"> <img src="img1.jpg"> <img src="img2.jpg"> </div> // after ajax completion <div id="ms"> <img src="img3.jpg"> <img src="img4.jpg"> </div> var ik = new array(); var ls = function(){ $('#ms img').each(function() { ik.push($(this).attr("src")); }); return ik; } var bs = function() { ls(); //img1.jpg, img2.jpg $(document).ajaxcomplete(function(event,request, settings){ ls(); //img1.jpg, img2.jpg, img3.jpg, img4.jpg should img3.jpg, img4.jpg }); return ik; }; console.log(bs());
i want before ajaxcompletion, array empty (img1.jpg , img2.jpg).
if no values:
ik = [];
for grateful.
first problem - repeating id's, should unique. after first call ls()
array ik
contain:
0: "img1.jpg" 1: "img2.jpg" 2: "img3.jpg" 3: "img4.jpg"
second problem - never reset array ik
, each successive call ls()
add same 4 values. though appears may have been debugging on part.
most importantly though, logging return value of bs
function (aptly named in case) should waiting ajaxcomplete called before logging results.
i'm not sure intention ik
array is, when using asynchronous calls should using callback pattern (or async pattern of choice).
something like:
var bs = function(callback) { $('document').ajaxcomplete(function(event, request, settings) { callback(…your result here…); }); }; bs(function(result) { console.log(result); });
Comments
Post a Comment