应设置超时触发多少次
how many times should setTimeout fires
我的 asp.net mvc网站中有以下代码:-
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 is a hack here, since the ".placeholders" don't exist yet
如今,MutationObserver 得到了广泛的支持。
var $container = $('.container'),
observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var placeholders = Array.prototype.filter.call(mutation.addedNodes, function(node) {
return node.className === 'placeholder';
});
if (placeholders.length > 0) console.log(placeholders);
});
});
observer.observe(document, {
childList: true,
subtree: true
});
var placeholders = setInterval(function() {
$container.append('<p class="placeholder">Hello world</p>');
}, 500),
goodbyes = setInterval(function() {
$container.append('<p class="goodbye">Goodbye world</p>')
}, 1000);
setTimeout(function() {
clearInterval(placeholders);
clearInterval(goodbyes);
}, 3500);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
</div>
-
loadImage()
必须被多次调用。 -
setTimeout()
在提供的延迟后调用提供的函数,只需一次。
相关文章:
- 检查表格提交了多少次
- 应设置超时触发多少次
- 链接超时会导致最后一次超时被拒绝
- 我需要在 500 次收集或我告诉它多少次收集后停止点击
- 使用输入多少次掷骰子,然后只输出最后一掷Javascript上的两个骰子的总和
- 我必须在外部脚本中包含 $(document) .ready (function() 多少次
- 如何找到突出显示的字符串有多少次出现,以及它是哪个出现次数
- 从 API 调用中,如何获取字段并查看该值重复了多少次
- 实例化函数对象时创建局部变量多少次?( 高级 )
- 我怎么能数出某个东西在我的对象中出现了多少次
- 试图跟踪多少次java脚本函数被调用
- 我如何找出多少次函数调用javascript/jquery
- jQuery:如果我在一个item类上调用.change(),它会被调用多少次?
- 检查一个URL包含某个字符多少次
- 衡量iOS每帧可以绘制多少次
- 多少次我必须调用http模块
- JS时间计算-一个日期在两个日期之间发生了多少次
- 在Angular非Angular网站上使用Protractor会出现多次超时
- 我怎么知道某个东西被点击了多少次?
- 我怎样才能知道一个“回答”了多少次?通过我的jquery显示/调用