我创建了一个循环来模拟我的数据,然后将其附加到选定的div中,我想确保在运行下一个函数之前已经完全模板化并注入了这些数据,所以我真正需要的是类似于回调函数。任何人都可以建议我如何做到这一点?
JS
// Template slideshow
for (var i = 0, len = data.films.length; i < len; i++) {
var dataPath = data.films[i];
var tmp = '<li><img src="http://dummyimage.com/300/000/' + dataPath.id + '" /><h2>' + dataPath.title + '</h2></li>';
$('.slides').append(tmp);
}
// Want to run this once Im sure all data has been templated and injected
$('.flexslider').flexslider({
animation: "slide"
});
答案 0 :(得分:2)
您不需要回调。您创建的代码始终在循环完成时注入。
答案 1 :(得分:1)
// Template slideshow
for (var i = 0, len = data.films.length; i < len; i++) {
var dataPath = data.films[i];
var tmp = '<li><img src="http://dummyimage.com/300/000/' + dataPath.id + '" /><h2>' + dataPath.title + '</h2></li>';
$('.slides').append(tmp);
// Want to run this once Im sure all data has been templated and injected
if (i==len) {
$('.flexslider').flexslider({
animation: "slide"
});
}
}