Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>鼠标在下面灰色区域移动,查看目标函数执行情况。</h2>

<p>在js代码块中,注释和打开对应的代码,来查看对应效果。可以同时打开控制台,查看目标函数执行的时间间隔。</p>

<div id="content"></div>

<p>【函数节流】连续触发事件但是在n秒中只执行一次函数。即 2n 秒内执行 2 次... 。节流如字面意思,会稀释函数的执行频率。</p>
              
            
!

CSS

              
                #content {
  height: 150px;
  line-height: 150px;
  text-align: center;
  color: #fff;
  background-color: #ccc;
  font-size: 80px;
}

              
            
!

JS

              
                var num = 1;
var content = document.getElementById('content');

var lastDate = new Date().getTime();

function count() {
    content.innerHTML = num++;

    var nowDate = new Date().getTime();
    console.log('目标函数执行间隔时间', nowDate-lastDate); // 输出两次相隔执行时间差
    lastDate = nowDate;
};

// 不做处理
// content.onmousemove = count;

// 延迟执行版
// content.onmousemove = throttle_defer(count,1000);

// 立即执行版
content.onmousemove = throttle_immediate(count,1000);

// 合成版 立即执行
// content.onmousemove = throttle(count,1000, true);


/**
 * @name 【函数节流】-立即执行版(时间戳)
 * @param func 目标函数
 * @param wait 延迟执行毫秒数
 */

function throttle_immediate(func, wait) {
  var previous = 0;
  return function() {
    var now = Date.now();
    var context = this; // 用来下面修改this指向
    var args = arguments; // arguments中存着event
    if (now - previous > wait) {
      func.apply(context, args);
      previous = now;
    }
  }
}


/**
 * @name 【函数节流】-延迟执行(定时器版)
 * @param func 目标函数
 * @param wait 延迟执行毫秒数
 */

function throttle_defer(func, wait) {
  var timer;
  return function() {
    var context = this; // 用来下面修改this指向
    var args = arguments; // arguments中存着event
    if (!timer) {
      timer = setTimeout(() => {
        timer = null;
        func.apply(context, args)
      }, wait)
    }
  }
}



/**
 * @desc 【函数节流】-合成版
 * @param func 函数
 * @param wait 延迟执行毫秒数
 * @param immediate true - 立即执行(时间戳版), false - 延迟执行(定时器版)
 */

function throttle(func, wait, immediate) {
  if (immediate) {
    var previous = 0;
  } else {
    var timer;
  }
  return function() {
    var context = this;
    var args = arguments;
    var args2 = Array.prototype.slice.call(arguments, 1);

    console.log('this', this);
    console.log('arguments', arguments);
    console.log('args2', args2);

    if (immediate) {
      var now = Date.now();

      if (now - previous > wait) {
        func.apply(context, args);
        previous = now;
      }
    } else {
      if (!timer) {
        timer = setTimeout(() => {
          timer = null;
          func.apply(context, args)
        }, wait)
      }
    }
  }
}


              
            
!
999px

Console