Working to a JS script, I wanted to add a pause in the execution of the script.
In PHP there is a function sleep() to delay execution of the PHP script, but JavaScript doesn't have such a function.
Looking on the net, I found a solution to delay the execution of some code in JS script, by using the setTimeout() function.

Here's the method, the code that must be executed after pause must be inside a function that will be accessed with setTimeout():

setTimeout( function() {
  // here add the code (or call a function) to be executed after pause
}, 1000 );
- 1000 represents the number of millisecond of the pause (for 2 seconds add 2000).

Here is an example, show two messages with alert(), the second alert will be displayed after a pause of 2 seconds.
<script type="text/javascript"><!--
function testPause(Pause) {
  alert('Hi there');
  setTimeout( function() {
    // code that must be executed after pause
    alert('Hi, I came after 2 seconds');
  }, Pause );


To test this example, click .

Here is another example that adds a "Loading ..." message into an HTML element, than, after 1.5 seconds displays a content.
<div id="cnt"> </div>
<script type="text/javascript"><!--
function simLoad(id) {
  // display 'Loading ..', and use setTimeout() to access a function after 1.5 seconds
  document.getElementById(id).innerHTML = '<h4>Loading ...</h4>';
  setTimeout(function() { adContent(id); }, 1500);

  /* in this case setTimeout() can be used also like this
      setTimeout("adContent('"+id+"')", 1500);

// this function adds a content inside a HTML element, with the ID passed in parameter
function adContent(id) {
  document.getElementById(id).innerHTML = 'JavaScript Tutorial - <i>Pause the script in JS</i> - <a href="" title="JavaScript Course">JavaScript Course</a>';

simLoad('cnt');        // access simLoad() with the ID of HTML tag

To test this example, click .

Daily Test with Code Example

Which attribute is used in <img> tag for the address of the image?
href src rel
<img src="" width="191" height="63" alt="Courses-Web" />
Which CSS code hides the element on page?
display: none; display: inline; position: relative;
#id {
  display: none;
What instruction stops the execution of a while() or for() statement?
continue prompt() break
for(var i = 0; i< 8; i++) {
  if(i > 1) break;
Indicate the function that can create a constant.
define() include() defined()
define("CONSTANT_NAME", "value");
Add Pause in JavaScript script

Last accessed pages

  1. Strategy Games (938)
  2. Check and Validate input field when loses focus, with PHP via Ajax (1913)
  3. Convert XML to JSON in PHP (2401)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (27189)
  5. HTML object and param (5585)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (27189)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (22785)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (22226)
  4. Read Excel file data in PHP - PhpExcelReader (19941)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (19261)