Vue equivalent of setTimeout?
Arrow Function
The best and simplest way to solve this problem is by using an arrow function () => {}
:
addToBasket() {
var item = this.photo;
this.$http.post('/api/buy/addToBasket', item);
this.basketAddSuccess = true;
// now 'this' is referencing the Vue object and not the 'setTimeout' scope
setTimeout(() => this.basketAddSuccess = false, 2000);
}
This works because the this
of arrow functions is bound to the this
of its enclosing scope- in Vue, that's the parent/ enclosing component. Inside a traditional function called by setTimeout
, however, this
refers to the window
object (which is why you ran into errors when you tried to access this.basketAddSuccess
in that context).
Argument Passing
Another way of doing this would be passing this
as an arg to your function through setTimeout
's prototype using its setTimeout(callback, delay, arg1, arg2, ...)
form:
addToBasket() {
item = this.photo;
this.$http.post('/api/buy/addToBasket', item);
this.basketAddSuccess = true;
//Add scope argument to func, pass this after delay in setTimeout
setTimeout(function(scope) {
scope.basketAddSuccess = false;
}, 2000, this);
}
(It's worth noting that the arg passing syntax is incompatible with IE 9 and below, however.)
Local Variable
Another possible, but less eloquent and less encouraged, way is to bind this
to a var outside of setTimeout
:
addToBasket() {
item = this.photo;
this.$http.post('/api/buy/addToBasket', item);
this.basketAddSuccess = true;
//Declare self, which is accessible inside setTimeout func
var self = this;
setTimeout(function() {
self.basketAddSuccess = false;
}, 2000);
}
Using an arrow function would eliminate the need for this extra variable entirely however, and really should be used unless something else is preventing its use.
Add bind(this) to your setTimeout callback function
setTimeout(function () {
this.basketAddSuccess = false
}.bind(this), 2000)
after encountering the same issue, I ended up on this thread. For future generation's sake: The current up-most voted answer, attempts to bind "this" to a variable in order to avoid changing the context when invoking the function which is defined in the setTimeout.
An alternate and more recommended approach(using Vue.JS 2.2 & ES6) is to use an arrow function in order to bind the context to the parent (Basically "addToBasket"'s "this" and the "setTimeout"'s "this" would still refer to the same object):
addToBasket: function(){
item = this.photo;
this.$http.post('/api/buy/addToBasket', item);
this.basketAddSuccess = true;
setTimeout(() => {
this.basketAddSuccess = false;
}, 2000);
}
ES6 can bind 'this'
setTimeout(() => {
},5000);