javascript - Checking url exists with jquery/ajax -


this has me totally confused. i'm trying run through series of youtube ids check if videos still exist.

i have ids in array , want check gdata api see if xml results or video not found 404 message.

i found function on check each id , add row table.

var ytarray = array of youtube ids var yttitles = array of matching titles vids var urlexists = function(url, callback){ $.ajax({             type: 'head',             url: url,             success: function() {                 callback(true);             },             error: function() {                 callback(false);             }                     });      }     var length = ytarray.length,     (var = 0; < length; i++) {     urlexists('http://gdata.youtube.com/feeds/api/videos/'ytarray[i], function(success) {         if (success) {               $('#rows').append('<tr><td>'+yttitles[i]+'</td><td>'+ytarray[i]+'</td><td style="color: green">found</td></tr>');         } else {               $('#rows').append('<tr><td>'+yttitles[i]+'</td><td>'+ytarray[i]+'</td><td style="color: red">not found</td></tr>');         }     });     }; }); }); 

the table fills out of rows contain information last video not each 1 goes through loop.

testing alerts seems table doesn't filled out until loops have completed.

i wonder if me not handling asynchronous nature of ajax correctly?

var ytarray   = ["array of youtube ids"],     yttitles  = ["array of matching titles vids"],     urlexists = function(url){         return $.ajax({             type: 'head',             url : url         });     },     output = function(state, i) {         var tr  = $('<tr />'),             td1 = $('<td />', {text : yttitles[i]}),             td2 = $('<td />', {text : ytarray[i]}),             td3 = $('<td />', {text : state ? 'found' : 'not found',                                 style: state ? 'color: green' : 'color:red'});          $('#rows').append( tr.append(td1, td2, td3) );     },     length = ytarray.length;  (var = 0; < length; i++) {     (function(k) {         urlexists('http://gdata.youtube.com/feeds/api/videos/' + ytarray[k])            .done(function() {              output(true, k);            }).fail(function() {                 output(false, k);         });     })(i); } 

took time figure out, you'll need closures, , different way of handling asynchronous behaviour.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -