Home > Web Front-end > JS Tutorial > body text

How can I create a 5-second delay in JavaScript before executing the next line of code?

Patricia Arquette
Release: 2024-10-25 17:07:03
Original
390 people have browsed it

How can I create a 5-second delay in JavaScript before executing the next line of code?

How to Wait 5 Seconds Before Executing the Next Line


When working in JavaScript, controlling the flow of execution is paramount. Consider the following scenario: you need a function to pause for 5 seconds before executing a specific action, such as checking a variable's value.


Using the setTimeout Function


Traditionally, JavaScript developers have relied on the setTimeout function to introduce delays in execution. This function takes two parameters: a string representing the code to execute and a delay expressed in milliseconds. In your particular case, the code excerpt below falls short:


<br>function stateChange(newState) {<br>  setTimeout('', 5000);</p>
<p>if (newState == -1) {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">alert('VIDEO HAS STOPPED');
Copy after login
Copy after login

}
}

The issue here is that the empty string passed to setTimeout doesn't have any effect. To create a 5-second delay before checking the newState variable, you should modify the code as follows:


<br>function stateChange(newState) {<br>  setTimeout(() => {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">if (newState == -1) {
  alert('VIDEO HAS STOPPED');
}
Copy after login

}, 5000);
}

By providing a function as the first argument, you specify the code to be executed after the delay. Now, the function will accurately pause for 5 seconds before checking the newState.


Alternative Solutions


In modern JavaScript, you can also leverage async/await to achieve the same result. Async/await simplifies asynchronous operations and makes your code more readable. Here's how to implement it for your scenario:


<br>const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));</p>
<p>async function stateChange(newState) {<br>  await delay(5000);</p>
<p>if (newState == -1) {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">alert('VIDEO HAS STOPPED');
Copy after login
Copy after login

}
}

This code employs the delay function, which returns a Promise that resolves after the specified delay. By using async/await, you declare the stateChange function as an asynchronous function and await the completion of the delay, ensuring the 5-second pause before checking the newState.

The above is the detailed content of How can I create a 5-second delay in JavaScript before executing the next line of code?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Previous article:How to Access Global Variables from Gmail\'s Content Script in a Chrome Extension? Next article:Here are some title options that fit the criteria: * Relative vs. Absolute Paths in JavaScript: When to Use Which? * JavaScript Paths: Absolute or Relative? A Guide to Performance and Security. * Wh
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!