[英]javascript infinitely loop over object every 5 seconds
I have a users object 我有一个用户对象
var users = {
one: {
name: 'batman',
age: 25
},
two: {
name: 'spiderman',
age: 27
},
three: {
name: 'superman',
age: 26
}
}
var displayName = function(name) {
console.log(name);
}
for(var name in users) {
if(users.hasOwnProperty(name)) {
displayName(name);
}
}
I want to loop over the object 'users' infinitely and call displayName
function on every 5 seconds. 我想无限地遍历对象“用户”,并每5秒调用一次
displayName
函数。 So that it will display one single name after every 5 seconds. 这样每隔5秒就会显示一个名称。
Where is the best place to place the setInterval
? 放置
setInterval
的最佳位置在哪里?
You have to populate array of names and iterate through it. 您必须填充名称数组并对其进行遍历。
And never don't use setInterval
, use setTimeout
instead. 而且永远不要使用
setInterval
,而应使用setTimeout
。
var aNames = []; // array of names
for(var name in users)
if(users.hasOwnProperty(name)) aNames.push(name);
var nNames = aNames.length, i = 0;
var displayNextName = function() {
console.log(aNames[i]);
i = (i + 1) % nNames;
setTimeout(displayNextName, 5000);
}
displayNextName();
UPDATE: 更新:
Extended version with support of break/stop of infinite looping: 扩展版本,支持无限循环的中断/停止:
var aNames = [];
for(var name in users)
if(users.hasOwnProperty(name)) aNames.push(name);
var nNames = aNames.length, i = 0;
var idTimeout;
var displayNextName = function() {
console.log(aNames[i]);
i = (i + 1) % nNames;
idTimeout = setTimeout(displayNextName, 5000);
}
var stopDisplayNextName = function() {
if (idTimeout) clearTimeout(idTimeout);
}
displayNextName();
It's easier if you change your users
object to be a simple array: 如果将
users
对象更改为一个简单数组,会更容易:
var users = [
{name: 'batman', age: 25},
{name: 'spiderman', age: 27},
{name: 'superman', age: 26}
];
UPDATE: ...but as you require users
to remain an object we can simply build our own array of users directly from the object: 更新: ...但是当您需要
users
保留对象时,我们可以直接从该对象直接构建我们自己的用户数组:
var users = {
one: {
name: 'batman',
age: 25
},
two: {
name: 'spiderman',
age: 27
},
three: {
name: 'superman',
age: 26
}
};
//Build an array of users from the users object
var userArray = [],
buildUserArray = (function(){
for (key in users) {
if (users.hasOwnProperty(key)){
userArray.push(users[key]);
}
}
})();
var displayNames = {
max : userArray.length,
current : 0,
timeout : null,
go : function() {
console.log(userArray[this.current]);//the whole user object
console.log(userArray[this.current].name);//just the name
this.current = (this.current+1) % this.max;//increment current position
this.timeout = setTimeout(function(){displayNames.go();}, 500);
},
stop : function(){
clearTimeout(this.timeout);
}
}
//to start
displayNames.go();
//set to stop after 5 seconds:
setTimeout(function(){displayNames.stop();}, 5000);
http://jsfiddle.net/9txjx9dy/1/ (updated) http://jsfiddle.net/9txjx9dy/1/ (已更新)
var users = {
one: {
name: 'batman',
age: 25
},
two: {
name: 'spiderman',
age: 27
},
three: {
name: 'superman',
age: 26
}
}
var i=0;
var displayName = function() {
if(i==1) {
console.log(users.one.name);
}
if(i==2) {
console.log(users.two.name);
}
if(i==3) {
console.log(users.three.name);
}
i= (i+1)%4;
}
setInterval(function () {displayName(name)}, 3000);
var users = {
one: {
name: 'batman',
age: 25
},
two: {
name: 'spiderman',
age: 27
},
three: {
name: 'superman',
age: 26
}
}
var displayName = function(name) {
console.log(name);
}
for(var name in users) {
if(users.hasOwnProperty(name)) {
setInterval(function () {displayName(name)}, 3000);
}
}
var users = {
one: {
name: 'batman',
age: 25
},
two: {
name: 'spiderman',
age: 27
},
three: {
name: 'superman',
age: 26
}
}
var i=0;
var displayName = function() {
console.log(users[i%3].name);
i= i+1;
}
setInterval(function () {displayName()}, 3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.