本文介绍了foreach循环与计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在javascript中使用计时器为每个循环编写一个。
I need to write a for each loop with timer in javascript.
它将以特定的时间间隔传递对象的每个索引。
假设我们有 obj = {a:1,b:2,c:3,...}
It will pass each index of the object with a specific time interval.
Lets say we have obj = {a:1,b:2,c:3,...}
time: 0ms => obj.a
time: 100ms => obj.b
time: 200ms => obj.c
.
.
.
我做了以下两件事,但不能将这两件事联合起来。
I have done the following two things but can't unite these two.
请查看
推荐答案
使用此:
var obj = {a:1, b:2, c:5, z:12, x:0};
var timer = 0;
for (var prop in obj) (function(key, val) {
setTimeout(function() {
$('#curr_elem').append( key + " => " + val + " | " );
}, timer += 1000);
})(prop, obj[prop]);
这篇关于foreach循环与计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!