0

HI here my scenario is i need call a method only after 1 min here i can also set time out but instead is it possible using delay method like below

getMethod(){
 
}


delay(1000,this.getMethod())

or any other better approach

Madpop
  • 729
  • 4
  • 24
  • 61

3 Answers3

4

If you are using rxjs and Angular then you probably want to use the RxJs timer subject

import { timer } from 'rxjs';
const minute = 1000 * 60;

function helloWorld() {
  console.log('hello');
}

timer(minute).subscribe(helloWorld);

If you are looking into what RxJs Subject or Operator you want, try checking the RxJs Operator Decision Tree for help.

blacksun1
  • 116
  • 4
2

you can use rxjs delay pipe:

getMethod(): Observable {
  return of(2)
}
const delayedMethod = getMethod.pipe(delay(1000))
delayedClicks.subscribe(x => console.log(x));
mehranmb78
  • 674
  • 5
  • 9
  • Doesn't this just delays the response? I mean, it doesn't actually delays the observable subscription, only holds the response for a while. – MumiaIrrequieta May 10 '23 at 14:35
0

You can use a promise & async-await to achieve this. Check below code

  const sleep = (milliseconds) => {
    return new Promise(resolve => setTimeout(resolve, milliseconds))
}

await sleep(2000) // This will give 2 seconds delay
Rajan
  • 1,512
  • 2
  • 14
  • 18