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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
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">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
setTimeout and this with bind() method in JavaScript class

Last accessed pages

  1. Disable button and Enable it after specified time (17532)
  2. Get Mime Type of file or string content in PHP (6229)
  3. PHP MySQL - using MySQLi (9669)
  4. Integer and Float value in Select with PDO from string to numeric (8658)
  5. Moving html element to a random direction (5214)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (63)
  5. CSS3 2D transforms (46)