Typescript sleep

前端 未结 7 560
后悔当初
后悔当初 2020-11-30 20:02

I\'m developing a website in Angular 2 using Typescript and I was wondering if there was a way to implement thread.sleep(ms) functionality.

My use case

7条回答
  •  春和景丽
    2020-11-30 20:27

    If you are using angular5 and above, please include the below method in your ts file.

    async delay(ms: number) {
        await new Promise(resolve => setTimeout(()=>resolve(), ms)).then(()=>console.log("fired"));
    }
    

    then call this delay() method wherever you want.

    e.g:

    validateInputValues() {
        if (null == this.id|| this.id== "") {
            this.messageService.add(
                {severity: 'error', summary: 'ID is Required.'});
            this.delay(3000).then(any => {
                this.messageService.clear();
            });
        }
    }
    

    This will disappear message growl after 3 seconds.

提交回复
热议问题