您可以使用setInterval
或链接的一系列setTimeout
. 我更喜欢后者,它看起来像这样:
function showValues(a, delay) {
var index = 0;
if (a.length !== 0) {
// If you want a delay before the first one:
setTimeout(showValue, delay);
// Alternately, if you want to show the first right away
// showValue();
}
// Function to show one value
function showValue() {
// Get the value
var value = a[index++];
/* ...show the value however you see fit... */
// Schedule next display, if any
if (index < a.length) {
// Schedule next run
setTimeout(showValue, delay);
}
}
}
用法:
showValues(["value1", "value2", /* etc. */, 2000); 2000 = two seconds
实例| 资源