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).

Example, executing a JS code after a specified time

The following script it writes a text in page (with document.write), then, after a pause of 2.3 seconds it will display an alert dialog window.

//executes some code after a 'pause' (in millisecond)
function testPause(pause){
 // code that must be executed after the pause
 alert('Hi, I came after 2.3 seconds');
 }, pause );

document.write('Hi there, wait');

Here is another example that adds a 'Loading ...' message into an HTML element, than, after 2.5 seconds it adds another content.
<div id='cnt'> </div>

function simLoad(id){
 // display 'Loading ..', and use setTimeout() to access a function after 2.3 seconds
 document.getElementById(id).innerHTML = '<h4>Loading ...</h4>';

 setTimeout(()=>{ adContent(id); }, 2500);

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

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

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

Daily Test with Code Example

Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1"></div></body>';
$dochtml = new DOMDocument();
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    //
Add Pause in JavaScript script

Last accessed pages

  1. array_map and array_search in JavaScript (1395)
  2. Working with HTML attributes in PHP (6701)
  3. XML data from ActionScript to a PHP script (1104)
  4. html2canvas - Save page screenshoot on server (1224)
  5. Laravel Middleware (136)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1976)
  2. Contact page - CoursesWeb (1924)
  3. Tabs effect with CSS (1924)
  4. Insert, Select and Update NULL value in MySQL (1228)
  5. PHP getElementById and getElementsByTagName (723)