站内搜索:
首页 >> 前端 >> 内容
前端节流功能实现教程

时间:2018/2/21 10:41:11

p点击事件执行后需要冷却1秒后再次点击才有效

<p id="p">0</p>

function throttle(handler, wait) {
    var lastTime = 0,
        nowTime;
    return function (e) {
        nowTime = new Date().getTime();
        if(nowTime - lastTime > wait) {
            handler.apply(this, arguments);
            lastTime = nowTime;
        }
    }
}

var p = document.getElementById('p');

function pClick() {
    this.innerHTML = parseInt(this.innerHTML) + 1;
}
p.onclick = throttle(pClick, 1000);

  • 上一篇:百度地图API:往地图中添加标注点的方法
  • 下一篇:webpack3新增的ScopeHoisting对比介绍
  • 返回顶部