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 definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
setTimeout and this with bind() method in JavaScript class

Last accessed pages

  1. Set custom message for required and field validation (1337)
  2. Tutorial Motion Presets (2513)
  3. How to use php variable in external js file (3711)
  4. Register Events to elements included with jQuery ajax (1282)
  5. Adding data from HTML Table Rows in Form fields (9588)

Popular pages this month

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