<!DOCTYPE html> <html> <head> <title></title> </head> <body> <input type="button" value="添加"> <ul></ul> <script> var oUl = document.querySelector("ul"); var oBtn = document.querySelector("input"); var count = 0; var timer = null; function createDom() { var oLi = document.createElement("li"); oLi.innerHTML = count++; oUl.appendChild(oLi); } var addDom = (function () { return new Promise(function (resolve, reject) { resolve(createDom); }); })(); oBtn.onclick = function () { timer = setInterval(function () { if (count == 10) { clearInterval(timer); } else { addDom.then(function (fn) { fn(); }); } }, 1000); } </script> </body> </html>
promise 的基本用法