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 defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
setTimeout and this with bind() method in JavaScript class

Last accessed pages

  1. Zodiac Signs PHP code (4041)
  2. Get the value of the selected /checked checkboxes in a form (42018)
  3. SHA1 Encrypt data in JavaScript (14279)
  4. Inheritance - Parent class and Child class (2448)
  5. querySelector and querySelectorAll (12508)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1584)
  2. Contact page - CoursesWeb (1547)
  3. Tabs effect with CSS (1541)
  4. PHP getElementById and getElementsByTagName (749)
  5. Get Attribute (ID, Class, Name, Title, Src) with jQuery (624)