Jordan Quiroz
Jordan Quiroz

Reputation: 95

stop function after x seconds angular 9

i want to stop a function in 10000 seconds but this doesnt work on angular:

setTimeout(() => {
        this
        .ngZone
        .run(() => {
            if (device.name.includes('DeviceName')) {
              this.connectToDevice(device)
            }
        });
       }, 10000,  console.log('not found'));

this start the function after 10000, but i need to stop the function after 10000 seconds and show a message..

Upvotes: 0

Views: 746

Answers (1)

AlleXyS
AlleXyS

Reputation: 2598

you can use pipe with timeout from rxjs

import { timeout } from 'rxjs/operators';

example:

your_subscription
  .pipe(timeout(10000)) // closed after 10 seconds
  .subscribe((result) => {
    // do stuff
  });

Upvotes: 6

Related Questions