其他分享
首页 > 其他分享> > promise间隔时间添加dom

promise间隔时间添加dom

作者:互联网

<!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 的基本用法

标签:function,count,dom,oLi,timer,promise,var,间隔时间,document
来源: https://blog.51cto.com/u_15200634/2774479