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>【函数防抖】短时间内多次触发同一事件,只执行最后一次(非立即执行),或者只执行最开始的一次(立即执行),中间的不执行。</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 = debounce_defer(count,1000);

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

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



/**
 * @name 【函数防抖】-延迟执行版
 * @param func 目标函数
 * @param wait 延迟执行毫秒数
 */

function debounce_defer(func, wait) {
  var timer;
  return function() {
    var context = this; // 用来下面修改this指向
    var args = arguments; // arguments中存着event

    if (timer) clearTimeout(timer);

    timer = setTimeout(function() {
      func.apply(this, args)
    }, wait)
  }
}



/**
 * @name 【函数防抖】-立即执行版
 * @param func 目标函数
 * @param wait 延迟执行毫秒数
 */

function debounce_immediate(func, wait) {
  var timer;
  return function() {
    var context = this; // 用来下面修改this指向
    var args = arguments; // arguments中存着event

    if (timer) clearTimeout(timer);
    
    var callNow = !timer;

    timer = setTimeout(function() {
      // 设置为null即下次callNow为true,即可实现下次执行。
      timer = null;
    }, wait)

    if (callNow) func.apply(context, args);
  }
}


/**
 * @name 【函数防抖】-合成版
 * @param func 目标函数
 * @param wait 延迟执行毫秒数
 * @param immediate true - 立即执行, false - 延迟执行
 */

function debounce(func, wait, immediate) {
  var timer;
  return function() {
    var context = this; // 用来下面修改this指向
    var args = arguments; // arguments中存着event
          
    if (timer) clearTimeout(timer);
    if (immediate) {
      var callNow = !timer;
      timer = setTimeout(function() {
        // 设置为null即下次callNow为true,即可实现下次执行。
        timer = null;
      }, wait);
      if (callNow) func.apply(context, args);
    } else {
      timer  = setTimeout(function() {
        func.apply(context, args);
      }, wait)
    }
  }
}

              
            
!
999px

Console