Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a time delay for a function call in Angular 5?

I want to give it some delay before calling the redirect URL in order to allow to store the needed data.

How can this be achieved?

like image 701
ruby Avatar asked Jul 13 '18 08:07

ruby


People also ask

How do I add a delay to a function call?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.

How do you delay a function call in TypeScript?

await new Promise(f => setTimeout(f, 1000)); Please note, that you can use await only inside async function. setTimeout( () => { /*Your Code*/ }, Milliseconds );


2 Answers

Simple solution is to use Router Service to redirect and setTimeout function for delay

    gotToPage() : void {
      // something
        setTimeout(() => this._router.navigateByUrl('url'),2500); // 2500 is millisecond
    }
like image 197
Muhammed Albarmavi Avatar answered Oct 03 '22 06:10

Muhammed Albarmavi


You have such decorator in the utils-decorators (npm install --save utils-decorators) lib:

import {delay} from 'utils-decorators';

class SomeComponent {

   @delay(1000)
   func(): any {
    ....
   }
}

You will find other very useful decorators there, such as debounce, throttle and many more.

https://vlio20.github.io/utils-decorators/#delay

like image 34
vlio20 Avatar answered Oct 03 '22 05:10

vlio20