SOURCE

/**
 * 给你一个数组根据时间按顺序每秒钟输出一个元素以及其下标
 */

function delayPrint(arr) {
    let index = 0;
    let timer = setInterval(() => {
        console.log(`item: ${arr[index]}, index: ${index++}`);
        if (index >= arr.length) clearInterval(timer);
    }, 1000);
}

function delayPrint2(arr) {
    let index = 0;
    let output = function() {
        setTimeout(function() {
            console.log(`item: ${arr[index]}, index: ${index++}`);
            if (index < arr.length) output();
        }, 1000);
    };
    output();
}

delayPrint2([5, 4, 3, 2, 1, 0, 'a']);
console 命令行工具 X clear

                    
>
console