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 tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert(""); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food = array("fruits" => array("banana", "apple"), "veggie" => array("collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Add Pause in JavaScript script

Last accessed pages

  1. Zuma Deluxe (19836)
  2. Twins (530)
  3. Accesing XML data - E4X (439)
  4. Zuma Games (15106)
  5. Insert, Select and Update NULL value in MySQL (13087)

Top accessed pages

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