Vue equivalent of setTimeout?

前端 未结 13 2305
栀梦
栀梦 2021-01-30 08:20

I\'m making a shopping cart system with Laravel and Vue. When I add an item to the basket, I display a confirmation message by toggling a Vue variable being watched by a v-if:

相关标签:
13条回答
  • 2021-01-30 08:27

    ES6 can bind 'this'

    setTimeout(() => {
    
     },5000);
    
    
    0 讨论(0)
  • 2021-01-30 08:33

    You can use Vue.nextTick

    addToBasket: function(){
                    item = this.photo;
                    this.$http.post('/api/buy/addToBasket', item);
                    this.basketAddSuccess = true;
                    Vue.nextTick(() =>{
                        this.basketAddSuccess = false;
                    });
                }
    
    0 讨论(0)
  • 2021-01-30 08:35

    It is likely a scope issue. Try the following instead:

    addToBasket: function(){
        item = this.photo;
        this.$http.post('/api/buy/addToBasket', item);
        this.basketAddSuccess = true;
        var self = this;
        setTimeout(function(){
            self.basketAddSuccess = false;
        }, 2000);
    }
    
    0 讨论(0)
  • 2021-01-30 08:37

    Kevin Muchwat above has the BEST answer, despite only 10 upvotes and not selected answer.

    setTimeout(function () {
        this.basketAddSuccess = false
    }.bind(this), 2000)
    

    Let me explain WHY.

    "Arrow Function" is ECMA6/ECMA2015. It's perfectly valid in compiled code or controlled client situations (cordova phone apps, Node.js), and it's nice and succinct. It will even probably pass your testing!

    However, Microsoft in their infinite wisdom has decided that Internet Explorer WILL NOT EVER SUPPORT ECMA2015!

    Their new Edge browser does, but that's not good enough for public facing websites.

    Doing a standard function(){} and adding .bind(this) however is the ECMA5.1 (which IS fully supported) syntax for the exact same functionality.

    This is also important in ajax/post .then/else calls. At the end of your .then(function){}) you need to bind(this) there as well so: .then(function(){this.flag = true}.bind(this))

    I would have added this as a comment to Kevin's response, but for some silly reason it takes less points to post replies than to comment on replies

    DO NOT MAKE THE SAME MISTAKE I MADE!

    I code on a Mac, and used the 48 points upvoted comment which worked great! Until I got some calls on my scripts failing and I couldn't figure out why. I had to go back and update dozens of calls from arrow syntax to function(){}.bind(this) syntax.

    Thankfully I found this thread again and got the right answer. Kevin, I am forever grateful.

    As per the "Accepted answer", that has other potential issues dealing with additional libraries (had problems properly accessing/updating Vue properties/functions)

    0 讨论(0)
  • 2021-01-30 08:38

    There's no need for bind(this) when you are using arrow functions:

      setTimeout( ()=> {
        // some code
       }, 500)
    
    0 讨论(0)
  • 2021-01-30 08:41

    Add bind(this) to your setTimeout callback function

    setTimeout(function () {
        this.basketAddSuccess = false
    }.bind(this), 2000)
    
    0 讨论(0)
提交回复
热议问题