【问题标题】:javascript loops: restore console after each iterationjavascript循环:每次迭代后恢复控制台
【发布时间】:2017-10-27 16:40:40
【问题描述】:

我目前正在编写在 Node.js 上运行时仅记录下面对象的每个值的代码,每次迭代延迟一秒,并与字符串“你现在正在观看”连接

const episodes = [
  { id: 's06e01', title: 'Pilot' },
  { id: 's06e02', title: 'Top Banana' },
  { id: 's06e03', title: 'Charity Drive' },
  { id: 's06e04', title: 'Visiting Ours' },
  { id: 's06e05', title: 'My Mother, the Car' },
  { id: 's06e06', title: 'In God We Trust' },
  { id: 's06e07', title: 'Storming the castle' },
  { id: 's06e08', title: 'Pier Pressure' },
  { id: 's06e09', title: 'Public Relations' },
];
    const finaleEpisode = { id: 's06e10', title: 'Bringing Up Buster' };

const addToPlaylist = episodes.concat(finaleEpisode)

下面是我设法做到这一点的地方,但我知道有一种更简单的方法可以做到这一点,我打算使用循环但无法找到在每次迭代后恢复控制台日志的方法。因此我使用了异步JS来延迟打印和清除功能

console.log("You are Now Watching "  + addToPlaylist[0].id + ' ' + addToPlaylist[0].title);


setTimeout(function(){console.log('\033c'); }, 3000);

setTimeout(function(){console.log("You are Now Watching "  + addToPlaylist[1].id + ' ' + addToPlaylist[1].title); }, 4000);

setTimeout(function(){console.log('\033c'); }, 5000);

setTimeout(function(){console.log("You are Now Watching "  + addToPlaylist[2].id + ' ' + addToPlaylist[2].title); }, 6000);

setTimeout(function(){console.log('\033c'); }, 7000);

setTimeout(function(){console.log("You are Now Watching "  + addToPlaylist[3].id + ' ' + addToPlaylist[3].title); }, 8000);

setTimeout(function(){console.log('\033c'); }, 9000);

setTimeout(function(){console.log("You are Now Watching "  + addToPlaylist[4].id + ' ' + addToPlaylist[4].title); }, 10000);

setTimeout(function(){console.log('\033c'); }, 11000);

setTimeout(function(){console.log("You are Now Watching "  + addToPlaylist[5].id + ' ' + addToPlaylist[5].title); }, 12000);

setTimeout(function(){console.log('\033c'); }, 13000);

setTimeout(function(){console.log("You are Now Watching "  + addToPlaylist[6].id + ' ' + addToPlaylist[6].title); }, 14000);

【问题讨论】:

标签: javascript node.js loops


【解决方案1】:
function doTask(num, max) {
    console.log("do whatever you want with " + num);
    if(num < max) {
        window.setTimeout(function() {
            doTask(++num, max);
        }, 2000);
    }
}

好的,这是递归的。不要以超过几百万的最大值进行操作。

【讨论】:

    【解决方案2】:

    我猜你正在寻找这样的东西:

    for (let i = 0; i < addToPlaylist.length; i++) {
        setTimeout(() => {
            // clears all characters printer on previous loop
            process.stdout.clearLine();
    
            // use \r at the end to return the cursor to the begining 
            process.stdout.write("You are Now Watching "  + addToPlaylist[i].id + ' ' + addToPlaylist[i].title + "\r");
        }, i * 2000);
    }
    

    您的解决方案并没有真正“恢复”通过console.log btw 打印的行。

    【讨论】:

    • 那我原来的代码到底在做什么?
    • 当我在节点中尝试时,它正在打印新行,起初它们不可见,但后来我尝试滚动一下,它们仍然存在。
    猜你喜欢
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多