Javascript Course

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){
 setTimeout(()=>{
 // code that must be executed after the pause
 alert('Hi, I came after 2.3 seconds');
 }, pause );
}

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

testPause(2300);
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>

<script>
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='//coursesweb.net/javascript' title='JavaScript Course'>JavaScript Course</a>';
}

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

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Add Pause in JavaScript script

Last accessed pages

  1. Shape Tween - Flash Animation (6046)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137629)
  3. Read Excel file data in PHP - PhpExcelReader (96715)
  4. jQuery plugin for Image SlideShow Carousel - Feature Carousel 2 (2225)
  5. array_map and array_search in JavaScript (4040)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (318)
  2. Read Excel file data in PHP - PhpExcelReader (116)
  3. The Four Agreements (96)
  4. PHP Unzipper - Extract Zip, Rar Archives (91)
  5. The Mastery of Love (85)
Chat
Chat or leave a message for the other users
Full screenInchide