javascript - Strange behavior while creating an image and video array (videos loading first or not at all) -
i have json feed (retrieved .ajax long polling) both movie , picture links. want "preload" images , videos, adding them array.
i want media objects added array in order in feed. have straight forward script loops through feed here encounter:
when write code add items array if specify videos first loaded front of array (ahead of images in feed added, though there images ahead of movies).
here code:
//json feed = http://app.sportstream.com/api/tagstream/tagstreamforstagetwomoderation?q=%7b%22customerid%22:%22ab.inch.2014%22,%22type%22:%7b%22$in%22:%5b%22image%22,%22video%22%5d%7d%7d&_=_ (it returns array of entries) (var k = 0, j = entries.length; k < j; k++) { var limit = 0; if (idcheck.indexof(entries[k].id) == -1) { if (++limit > 75) return; idcheck.push(entries[k].id); var mediaobject = new custommedia(); //placing here load videos in feed front of array if (entries[k].type == 'video') { mediaobject.isvideo = true; mediaobject.url = entries[k]['ssmetadata']['videos']['standard_resolution']['url']; console.log('new video loaded'); contentarraynew.push(mediaobject); if (!preloaddone && boxcount != null) { if (contentarraynew.length >= boxcount) { preloaddone = true; imagesloaded(); } } //checking if type == 'image' before video prevents videos loading } else if (entries[k].type = 'image') { mediaobject.isvideo = false; mediaobject.url = entries[k]['ssmetadata']['images']['standard_resolution']['url']; var img = new image(); img.onload = (function (entry) { return function () { contentarraynew.push(entry); console.log('new image loaded'); if (!preloaddone && boxcount != null) { if (contentarraynew.length >= boxcount) { preloaddone = true; imagesloaded(); } } } })(mediaobject); img.onerror = function () { console.log('error: bad image source'); }; img.src = mediaobject.url; } else { console.log('invalid media format'); } } }
Comments
Post a Comment