0

i have an input field where i can write in something, now if i press enter a value called "gespeichert" gets true. i also have an p tag that is binded with v-if. now i want it to hide / remove it after 2 or 3 seconds. this is maded with vue.js

i already tried with

                    methods: {

        speichern: function() {
            this.gespeichert = true;
            setTimeout(function(){

                    this.gespeichert = false;

            }, 2000);
                             ....

now i want that gespeichert gets the value false after 2 seconds, why does this not work?

bill.gates
  • 14,145
  • 3
  • 19
  • 47

1 Answers1

2

You have a scope issue - this within your setTimeout function is not what it is outside of that function. You can use .bind(this) to solve this:

setTimeout(function(){
    this.gespeichert = false;
}.bind(this), 2000);
ceejayoz
  • 176,543
  • 40
  • 303
  • 368