


Solution to the problem of setTimeout in javascript_javascript skills
May 16, 2016 pm 04:49 PMI saw a problem that probably looked like this.
name = 'out of you'
foo = function(){
this.name = 'xxoo';
}
foo.prototype.say = function(){
console.log(this.name);
}
f = new foo();
f.say(); // This sentence will output xxoo
setTimeout(f.say, 500); // This sentence will output out of you
This is a pitfall. JavaScript's this is generated when it is called, and it is also related to the context. This is how to solve it. I tested it and used call.
setTimeout.call(foo(), f.say, 500)
Some solutions on the Internet
This points to the problem of setTimeout in js
Using Timer in JavaScript
In the end, it’s still a matter of understanding this.
I can continue writing when I understand it better one day

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to get HTTP status code in JavaScript the easy way

How to use insertBefore in javascript
