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

Click on the tag that creates a cell in table
<tr> <span> <td>
Indicate the CSS property used to specify a background image for an element
background-color background-image color
h3 {
  background-image: url("image.jpg");
What instruction can be used to parse all the array items?
for() array() object()
var arr = [1, "ab", ""];
for(var i=0; i< arr.length; i++) { alert(arr[i]); };
Indicate the PHP instruction used to traverse an associative array.
for() foreach() if()
$arr = array("k1"=>"v1", "k2"=>"v2", "k3"=>"v3");
foreach($arr AS $k => $v) { echo "<br/>". $k ." - ". $v; }
Add Pause in JavaScript script

Last accessed pages

  1. PHP PDO - Select query, fetch (1437)
  2. Multiple Select Dropdown List with AJAX (1788)
  3. EasyPhpThumbnail Class (90)
  4. Recursive function to create Multi-Level Menu in JavaScript (91)
  5. Building and editing HTML webpage (157)

Top accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (3437)
  2. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (3288)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (3258)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (3042)
  5. PHP Chat Script (2708)