javascript - 点击数值累加
迷茫
迷茫 2017-04-11 10:36:57
0
2
260

点击计数时,用户有可能点击过快,数值飞速增加。
如果去把数值的增加控制在1秒内只能增1个值,点击多的不算。

计数: 点击 
    巴扎黑

    这种操作类型叫节流阀,我写个简单的例子吧:

    var throttle = function(func, wait) { var createThrottle = function(f, t) { var last, timer; return function() { var args = Array.prototype.slice.call(arguments); var _this = this, now = new Date().getTime(); if (typeof last === 'undefined') { last = now; return func.apply(_this, args); } if (now - last > wait) { func.apply(_this, args); last = new Date().getTime(); }else { clearTimeout(timer); timer = setTimeout(function() { func.apply(_this, args); last = new Date().getTime(); }, wait - now + last); } }; }; return createThrottle(func, wait); }; var num = 0; document.getElementById('countspan').innerHTML = num; function countNumber(){ num++; document.getElementById('countspan').innerHTML = num; } var throttled = throttle(countNumber, 1000);

    完整的demo,到plunker去看

      Popular Topics
      More>
      Popular Articles
      Popular Tutorials
      More>
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!