Javascript Course


setTimeout() can be used to call a function after a specified time, and to auto-call a function multiple times, if it is added inside it. But, setTimeout() causes javascript to use the global scope, and this can be a problem when it is used inside a class, because it cannot be used with the special word this.


For example, in the following code, setTimeout() not works as we expect.
<h4>Example this in setTimeout()</h4>

<div id="swprop"></div>
<script>
// defines a class function
class className {
 constructor(){
 this.prop = 0;
 }

 // method
 metName() {
 document.getElementById('swprop').innerHTML = this.prop; // shows prop value in #swprop

 // adds one unit to prop to each call, and auto-calls this function every 0.5 sec., till prop reaches 10
 this.prop++;
 if(this.prop < 10) setTimeout('this.metName()', 500);
 }
}

// creates an object of className, and accesses metName()
var objTest = new className();
objTest.metName();
</script>
If you test the above code with Chrome browser, in JavaScript Console will appear this error:
TypeError: this.metName is not a function
Which means the script looks for metName() in the Window object. The "this" we used in setTimeout() is scoping via itself.

Solution with the bind() method

The solution is to apply the bind(val) method, which has its this keyword set to the provided value (val).
- So, in setTimeout() use this syntax:

setTimeout(this.methodName.bind(this), milliseconds);

Applying this solution in the code above, the following example will work.
<h4>Example bind() method in setTimeout()</h4>

<div id="swprop"></div>
<script>
// defines a class function
class className {
 constructor(){
 this.prop = 0;
 }

 // method
 metName() {
 document.getElementById('swprop').innerHTML = this.prop; // shows prop value in #swprop

 // adds one unit to prop to each call, and auto-calls this function every 0.5 sec., till prop reaches 10
 this.prop++;
 if(this.prop < 10) setTimeout(this.metName.bind(this), 500);
 }
}

// creates an object of className, and accesses metName()
var objTest = new className();
objTest.metName();
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
}
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
alert(url);
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("http://coursesweb.net/");
echo $homepage;
setTimeout and this with bind() method in JavaScript class

Last accessed pages

  1. Moving html element to a random direction (5239)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (143119)
  3. PHP MySQL - WHERE and LIKE (29530)
  4. CSS Outline (2683)
  5. PHP Error Handling and Debugging (4457)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (352)
  2. CSS cursor property - Custom Cursors (41)
  3. The Mastery of Love (39)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (34)
  5. Read Excel file data in PHP - PhpExcelReader (31)