Cha*_*uza 1 html javascript css jquery
我有一个像这样的javascript对象:
var object = [{ id: 1, title:"xyz" }, {id: 2, title: "abc"}, {id: 3, title: "sdfs"}];
Run Code Online (Sandbox Code Playgroud)
现在我要做的是通过对象,它将读取第一个id并输出"xyz",然后暂停5秒,然后通过第二个id输出"abc",再次暂停5秒然后去通过第三个输入"sdfs",再次暂停5秒,然后从条目1开始.我想让它无限期地继续下去.任何帮助,将不胜感激.
你的基本递归函数:
function recursive(obj,idx) {
if (obj[idx]) {
alert(obj[idx].title);
setTimeout(function(){recursive(obj,idx+1);}, 5000); // milliseconds
};
};
recursive(myObject,0);
Run Code Online (Sandbox Code Playgroud)
或者,无限循环:
function recursive(obj,idx) {
if (obj[idx]) {
alert(obj[idx].title);
setTimeout(function(){recursive(obj,idx+1);}, 5000); // milliseconds
} else {
recursive(obj,0);
};
};
recursive(myObject,0);
Run Code Online (Sandbox Code Playgroud)