Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
How to set loop with pause in between each log

How could I make a loop that pauses for a set amount of time before repeating? I tried using setTimeout inside of a while loop but that didn't work.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

JavaScript is monothreaded, so blocking is to be avoided, that is why so much effort was pourred into the language to standardise Promise and add async / await

This allow to pause code execution without blocking, here how you do it:

// Only available in Node v15+
import { setTimeout as pause } from 'timers/promises'

// if not in node:
const pause = delay => new Promise(s => setTimeout(s, delay))

async function sayHelloEverySeconds() {
  while (true) {
    await pause(1000) // 1 sec
    console.log('hello')
  }
}

sayHelloEverySeconds()

But for this exact function, it would be simpler to use setInterval directly:

setInterval(() => console.log('hello'), 1000)

this will execute the function every seconds

about 4 years ago · Juan Pablo Isaza Report

0

You don't need to use async/await functions or promises or a while loop. Call a function and have setTimeout call the function again after a set amount of time.

function loop(count = 0) {
  console.log(count);
  setTimeout(loop, 1000, ++count);
}

loop();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!