Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function after 5 seconds?

I want to call a function executeAfter5secs() in my home.ts when home.html is opened. How do I do this ?

Currently I have added a button on home.html and on click of it, the function is being called, but I want to be free from the click and want to call it automatically once the page is loaded, but after 5 seconds.

How do I code in home.ts side, I am bit confused like what do I code inside:

ngOnInit() {    }

so that once page is loader time starts and after 5 seconds my function is called.

like image 675
user2828442 Avatar asked Feb 22 '18 10:02

user2828442


2 Answers

Add this code in your .ts file

ionViewDidLoad(){
    setTimeout(() => {
        alert('Hello...')
    }, 5000);
}
like image 147
AddWeb Solution Pvt Ltd Avatar answered Nov 11 '22 04:11

AddWeb Solution Pvt Ltd


you need to use 'AfterViewInit' after component's view fully initialized.

in .ts file

export class LoginPage implements AfterViewInit {

ngAfterViewInit(){
   setTimeout( ()=>{
   console.log('works')
   }, 5000)
 }

}
like image 28
Prasanth S Avatar answered Nov 11 '22 05:11

Prasanth S