Working with Timers
Complete the function "logString" in the editor below. 'logString" has the following parameters:
input:stringToLog
Write a timer function to log "Logging {stringToLog} every 0.5 seconds" 0.3 seconds after the server is started.
Write a timer function to log "{stringToLog}" every 0.5 seconds and the string should log 5 times.
Once the code is written, click Run Tests to run the test cases.
Complete the function "logString" in the editor below. 'logString" has the following parameters:
input:stringToLog
Write a timer function to log "Logging {stringToLog} every 0.5 seconds" 0.3 seconds after the server is started.
Write a timer function to log "{stringToLog}" every 0.5 seconds and the string should log 5 times.
Once the code is written, click Run Tests to run the test cases.
input:stringToLog
Write a timer function to log "Logging {stringToLog} every 0.5 seconds" 0.3 seconds after the server is started.
Write a timer function to log "{stringToLog}" every 0.5 seconds and the string should log 5 times.
Once the code is written, click Run Tests to run the test cases.
Solution:
function logString(stringToLog) {
let counter = 0;
function printSetTimeout() {
console.log(`Logging ${stringToLog} every 0.5 seconds`);
}
setTimeout(printSetTimeout, 300);
let timer = setInterval(function () {
counter++;
if (counter === 5) clearInterval(timer);
console.log(stringToLog);
}, 500);
}
Comments
Post a Comment