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

              
                <p>说明:鼠标在以下元素不断移动,将会不断执行一个数值累加事件,但中间分别加入了防抖和节流函数。</p>

<h2>防抖</h2>
<p>在鼠标停止移动后300ms执行一次数值累加事件。</p>
<div id="content">0</div>
<h2>节流</h2>
<p>在鼠标移动过程中,每300ms执行一次数值累加事件。</p>
<div id="content2">0</div>
              
            
!

CSS

              
                div{height:150px;line-height:150px;text-align:center; color: #fff;background-color:#ccc;font-size:80px;}
h2{margin: 10px 0;}
p{color:#666;margin:0;}
              
            
!

JS

              
                // 防抖函数
function debounce(func, wait) {
  let timeout = null;
  return function () {
    let context = this;
    let args = arguments;
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(context, args)
    }, wait);
  }
}

// test debounce
let num = 1;
let content = document.getElementById('content');
function count() {
  content.innerHTML = num++;
};
content.onmousemove = debounce(count, 300);



// 节流函数
function throttle(func, wait) {
  let timeout = null;
  return function () {
    let context = this;
    let args = arguments;
    if (!timeout) {
      timeout = setTimeout(() => {
        timeout = null;
        func.apply(context, args)
      }, wait)
    }
  }
}

// test throttle
let num2 = 1;
let content2 = document.getElementById('content2');
function count2() {
  content2.innerHTML = num2++;
};
content2.onmousemove = throttle(count2, 300);
              
            
!
999px

Console