2017-10-27 1 views
0

ich aktuell Code arbeite, dass, wenn läuft auf Node.js unten nur jeden des Wertes des Objekts protokolliert, um eine Sekunde bei jeder Iteration verzögert und mit der Zeichenfolge „Sie verkettet sind jetzt „javascript Schleifen: restore Konsole nach jeder Iteration

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) 

Below beobachten ist, wo ich nur das tun verwaltet, aber ich bin bewusst, dass es eine viel einfachere Art und Weise, dies zu tun ist, ich sollte eine Schleife verwenden, war aber nicht in der Lage, einen Weg finden, um die Konsole zu restaurieren log nach jeder Iteration. Daher verwendete ich Asynchronous JS die Druck- und Clearing-Funktionen

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); 
+0

Es ist mir nicht klar, was das Ziel dieser Übung -Sounds wie ein X/Y-Problem ist, oder vielleicht ein Duplikat https://stackoverflow.com/questions/24293376/javascript-for-loop-with-timeout – mplungjan

+1

Was bedeutet "Konsolenprotokoll wiederherstellen"? –

Antwort

0

Ich glaube, Sie suchen etwas wie folgt aus:

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); 
} 

Ihre Lösung war nicht wirklich „Wiederherstellen“, um die Leitung über console.log gedruckt btw.

+0

Was tat dann mein Originalcode wirklich? – thespaceman

+0

Als ich das im Knoten versuchte, druckte es neue Zeilen, sie waren zuerst nicht sichtbar, aber dann habe ich versucht, ein wenig zu scrollen und sie waren immer noch da. –

1
function doTask(num, max) { 
    console.log("do whatever you want with " + num); 
    if(num < max) { 
     window.setTimeout(function() { 
      doTask(++num, max); 
     }, 2000); 
    } 
} 

Ok zu verzögern, ist dies rekursiv. Tun Sie es nicht mit Max größer als einige Millionen.

Verwandte Themen