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>1、鼠标在右侧灰色区域移动,查看鼠标移动事件下控制台中输出的目标函数执行间隔。</p>
<p>2、滚动页面,查看页面滚动事件下控制台中输出的目标函数执行间隔。</p>

<p>因正常显示器刷新频率为60Hz(每秒60次),所以显示器刷新的最小间隔为1000/60(约等于16ms)。所以函数节流的最佳时间间隔推荐16ms。</p>

<p>【注:最好打开codepen的全屏模式(Full Page View)查看控制台中输出的执行间隔】</p>

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

CSS

              
                body {
  height: 5000px;
}
p {
  width: 65%;
}
#content {
  position: fixed;
  top: 0;
  right: 0;
  width: 30%;
  height: 150px;
  line-height: 150px;
  text-align: center;
  color: #fff;
  background-color: #ccc;
  font-size: 30px;
}

              
            
!

JS

              
                var num = 1;
var content = document.getElementById('content');
var lastDate = new Date().getTime();

// 鼠标移动事件
function count1() {
  content.innerHTML = '鼠标移动事件' + num++;

  var nowDate = new Date().getTime();
  // 输出两次相隔执行时间差
  console.log(nowDate-lastDate)
  lastDate = nowDate
};
content.onmousemove = throttle(count1, 0);


// 页面滚动事件
function count2() {
  content.innerHTML = '面滚动事件' + num++;

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

window.onscroll = throttle(count2, 0);

/**
 * @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;
    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