好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

TypeScript 实现 sleep 函数

看过不少网友的文章, 有各种方法, 但我想要的是一个能线性执行的sleep函数.

     /*  *
     * 等待指定的时间
     * @param ms
       */  
    static async sleep(ms: number) {
          return   new  Promise((resolve) =>  {
            setTimeout(()  =>  {
                resolve( '' );
            }, ms)
        });
    } 

 

调用方法:

 async test() {
      //   等待500毫秒后再执行同步 
    await sleep(500 );
    console.log( '123' );
} 

 

查看更多关于TypeScript 实现 sleep 函数的详细内容...

  阅读:89次