0
我有我的asp.net mvc的网站里面的下面的代码: -应该有多少次的setTimeout火灾
function loadImage(src, callback) {
var img = $('<img>').on('load', function() {
callback.call(img);
});
img.attr('src', src);
var allcaptions = $("figure span");
// setTimeout is a hack here, since the ".placeholders" don't exist yet
setTimeout(function() {
alert(1);
$(".placeholder").each(function (i) {
// in each .placeholder, copy its caption's mark-up into it (remove the img first)
var caption = allcaptions.eq(i).clone();
//caption.find("img").remove();
var t = caption.find("img").attr('data-goto');
// caption.append($("<a />", { "text": "test", "href": t }));
if (!(t == null || t == "undefined")) {
caption.append("<br/>");
caption.append("<a href='" + t + "' style='font-size:16px;font-weight:bold'>Read More</a>");
}
caption.find("img").remove();
$(this).append("<div class='caption'>" + caption.html() + "</div>");
});
}, 500);
alert(2)
}
现在根据我的理解是,settimout将大火后500毫秒,并会调用该函数。但到底发生了什么情况如下: -
当函数被调用(显示图片库时),我会收到以下提示: -
2
1
2
2
1
1
所以可以在此请,请问有人建议setTimeout的作品只会触发一次或执行多次?当然我添加仅用于测试目的的警报......
感谢
setTimeout只是在您定义N毫秒后执行一次函数。可能您的应用程序正在多次调用您的loadImage函数... –
不要使用警报进行调试。使用控制台 – epascarello
如果您想在n秒后一直重复调用您的函数,请使用'setInterval' – Rash