Avatar ·

How to make a pause (sleep) in JavaScript? Analog of sleep() from other languages

Hello! I'm new to JavaScript, transitioning from Python and PHP. There is a convenient sleep() function that pauses code execution for a specified number of seconds. For example: ```python print("Start") time.sleep(3) print("3 seconds passed") ``` In JavaScript, I tried to write something similar: ```javascript console.log("Start"); setTimeout(() => {}, 3000); // Hoped this would work console.log("3 seconds passed"); ``` But the code executes instantly, without any delay. I understand that setTimeout is an asynchronous function, but how can I make a simple synchronous pause so that the code stops and waits? I've seen that there are functions like `sleep` in Node.js, but they don't work in the browser. I tried while loops with time checking, but they freeze the browser. Please advise how to do it correctly.