我在for循环中添加类似setTimeout的功能时遇到了问题 - 我用它来迭代父div的每个子元素,然后在其中为每个子元素添加一个类,它添加了一个fadeIn,我想在添加每个类
后添加一个延迟
var children = $("#phototitle span");
for (var i = 0; i <= children.length; i++) {
var x = children[i]
$("#phototitle span:nth-of-type(" + i + ")").addClass("flip-in-hor-bottom");
console.log(x);
}
&#13;
setTimeout似乎不起作用,它只是将类添加到第一个或最后一个子元素。
由于
答案 0 :(得分:0)
您可以尝试这样的事情:
// Get all elements
var children = $('#phototitle span');
// For each element
children.each(function(i, e) {
// Get the element, and sets the delay for the element
var element = $(e),
delay = i * 1000; // One second delay = 1000 ms
// Set the timeout for the element
setTimeout(delay, function() {
element.addClass('flip-in-hor-bottom');
});
});
我认为这样做会有所帮助。
答案 1 :(得分:0)
您可以创建一个函数来添加类并在移动到下一个之前设置超时,具体如下:
<div id="container">
<div class="child">Child One</div>
<div class="child">Child Two</div>
<div class="child">Child Three</div>
<div class="child">Child Four</div>
<div class="child">Child Five</div>
<div class="child">Child Six</div>
</div>
&#13;
Dim formatter = new JsonSerializerSettings() With { .DateFormatString = "yyyy-MM-ddThh:mm:ssZ" }
Dim json = JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented,formatter)
&#13;
Dim ds = JsonConvert.DeserializeObject(Of DataSet)(json,formatter)
&#13;
答案 2 :(得分:0)
您可以使用以下内容。它是自调用函数,每1秒后调用一次,直到i
成为children.length
(直到它遍历所有必需节点)
var children = $("#phototitle span");
(function myLoop (i) {
setTimeout(function () {
$("#phototitle span:nth-of-type(" + i + ")").addClass("white");
if (i++ < children.length){
myLoop(i);
}
}, 1000)
})(1);
&#13;
.white{
color: white;
background-color: black;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="phototitle">
<span>1</span><br />
<span>2</span><br />
<span>3</span><br />
<span>4</span><br />
<span>5</span>
</div>
&#13;