Home » Javascript » Vuejs2 set timeout fails to execute the function after the set seconds

Vuejs2 set timeout fails to execute the function after the set seconds

Posted by: admin August 19, 2018 Leave a comment

Questions:

AM trying to execute a function after 1 second using setTimeout but it fails to work

so i have

mounted(){
   this.getTime();
}

methods:{
 getTime(){

    setTimeout(()=>{
     console.log("test..");
    },1000)
   }
}

THe above only logs once

Ihave tried changing the arrow function in set timout to

setTimeout(function(){
  console.log("test")
 }, 1000);

But even so it fails

Where am i going wrong as i expect more than one print of test to the console

I prefer using the mounted hook as this function will perform other operations with this keyword which becomes available after mount

Answers: