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 .
Add Pause in JavaScript script

Daily Test with Code Example

Which tag is used to include external CSS file in web page?
<body> <script> <link>
<link href="/templ/style.css" rel="stylesheet" type="text/css" />
Which CSS property sets the text size?
font-weight text-decoration font-size
h2 {
  font-size: 1em;
Indicate the JavaScript property that can add HTML code into an element.
text value innerHTML
document.getElementById("someID").innerHTML = "HTML content";
Click on the function that returns the number of characters of a string in PHP.
count() strlen() stristr()
$str = "";
$nr_chr = strlen($str);
echo $nr_chr;       // 22

Last accessed pages

  1. Animating in Flash - Frame-by-Frame Animation (1484)
  2. Qwop (166)
  3. PHP Strings (1794)
  4. Functions, Variable scope and Passing by Reference (909)
  5. Add, Change, and Remove Attributes with jQuery (18946)

Top accessed pages

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