約束方式非同期+待ちます
機能スリープ(持続時間){ 戻り 、新たな約束(関数(解決){ たsetTimeout(決意、持続時間); }) } 非同期関数changeColor(持続時間、色){ のdocument.getElementById( "トラフィックライト")style.background = カラー; 睡眠(デュレーション)を待ちます。 } 非同期関数のmain(){ ながら(真){ changeColor待つ( 3000、 "グリーン" )。 changeColor待つ( 1000、 "黄色" ); 待つchangeColor( 2000、 "赤"); } } )(主
オリジナルの方法
関数色(){ にconsole.log( 'グリーン' )。 setTimeout(() => { にconsole.log( '黄色' ); のsetTimeout(() => { にconsole.log( '赤' ); のsetTimeout(色、 2000 ); }、 1000年) }、 3000 )。 } 色();
ます。https://www.cnblogs.com/moli-/p/11124298.htmlで再現