skip to Main Content

I want to create a function that prints ‘Hello, World!’ every second for 5 minutes, and after 5 minutes, this function will stop for 1 minute. I want to create a loop like this with setTimeout and setInterval, but I couldn’t. Can anyone help me?

const helloWorld = () => console.log("Hello, World!")

const stopSayHello = () => console.log("Stopped for a minute!")

The HelloWorld function will run every second for 5 minutes. After 5 minutes, the stopSayHello function will run for a minute. Then, this loop will continue.

4

Answers


  1. You can have a function startTimer that will initiate the procedure…

    it will use setInterval to invoke helloWorld each second.. while keeping track of the number of times it occurred. When such counter hits 5*60 (1 minute) it clears the interval and prints it will be waiting for 1 minute.. by doing setTimeout and restarting the whole startTimer after such amount of time.

    The question is: such approach will overflow the call stack? Legit question but the setTimeout is expected to return immediately anwyay so there’s no infinite calls through the stack.

    const helloWorld = () => console.log("Hello, World!")
    const stopSayHello = () => console.log("Stopped for a minute!")
    
    var seconds = 0;
    
    var timer;
    startTimer();
    
    function startTimer(){
      timer = setInterval(()=>{
        seconds += 1;  
        helloWorld();
        if (seconds == 5*60){
          seconds = 0;
          clearInterval(timer);
          stopSayHello();
          setTimeout(startTimer, 1000*60);
        }
      }, 1000);
    }
    Login or Signup to reply.
  2. Here we go:

    const minutes5 = 300000, minutes1 = 60000, sec1 = 1000;
    const printHelloWorld = () {
      console.log('Hello, World!');
    }
    const stopIntervalTimer = (timerID) => {
        clearInterval(timerID);
    }
    const run = () {
      // Run the loop for 5 minutes
      const loopIntervalID = setInterval(printHelloWorld, sec1);
      setTimeout(() => {
        // Stop the loop after 5 minutes
        stopIntervalTimer(loopIntervalID);
        console.log('Loop stopped. Waiting for 1 minute...');
        
        // Wait for 1 minute before starting the loop again
        setTimeout(() => {
          console.log('Resuming loop...');
          run();
        }, minutes1);
      }, minutes5);
    }
    
    run();
    
    Login or Signup to reply.
  3. I add code below.

    const helloWorld = () => console.log("Hello, World!");
    const stopSayHello = () => console.log("Stopped for a minute!");
    
    const runLoop = () => {
      let timePassed = 0;
    
      // Run helloWorld every second until 5 minutes pass
      const helloInterval = setInterval(() => {
        helloWorld();
        timePassed += 1000;
    
        // After 5 minutes, stop helloWorld and run stopSayHello
        if (timePassed >= 5 * 60 * 1000) {
          clearInterval(helloInterval);
          stopSayHello();
    
          // After 1 minute, restart the loop
          setTimeout(runLoop, 60 * 1000);
        }
      }, 1000);
    };
    
    // Start the loop
    runLoop();
    Login or Signup to reply.
  4. const helloWorld = () => console.log("Hello, World!");
    const stopSayHello = () => console.log("Stopped for a minute!");
    
    const runHelloWorld = () => {
      // Run helloWorld every second for 5 minutes
      const helloInterval = setInterval(() => helloWorld(), 1000);
      
      // After 5 minutes, stop saying hello and wait for 1 minute
      setTimeout(() => {
        clearInterval(helloInterval);  // Stop the helloWorld interval
        stopSayHello();               // Print the stop message
        // After 1 minute, restart the loop
        setTimeout(() => runHelloWorld(), 60000);
      }, 300000);  // 5 minutes in milliseconds
    };
    
    // Start the loop
    runHelloWorld();

    This code sets up an initial setInterval to call the helloWorld function every second. After 5 minutes (300,000 milliseconds), it clears the interval, prints the stop message, and sets a setTimeout to restart the loop after 1 minute (60,000 milliseconds). This process repeats indefinitely.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search