文章博客

技术团队文档示例


前端2

<h4>题目1.如何实现sleep的效果(es5或者es6)</h4> <p><strong>(1)通过Promise来实现</strong></p> <pre><code class="language-javascript">function sleep(ms){ var temple = new Promise((resolve)=&gt;{ console.log(111); setTimeout(resolve,ms) }); return temple } sleep(500).then(function(){ console.log(222) }) //先输出111,延时500ms后输出222</code></pre> <p><strong>(2) 通过async封装</strong></p> <pre><code class="language-javascript">function sleep(ms){ return new Promise((resolve)=&gt;setTimeout(resolve,ms)); } async function test(){ var temple = await sleep(1000); console.log(111) return temple } text();//延迟1000ms输出1111</code></pre>

页面列表

ITEM_HTML