Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

第 102 期(内功-性能):节流 #105

Copy link
Copy link
@wingmeng

Description

@wingmeng
Issue body actions

节流是一种常用的性能优化手段,主要原理是:当持续触发事件时,对事件响应进行抽稀,保证一定时间段内只调用一次事件处理函数。

节流函数:

function throttle(fn, wait) {
  var prev = 0;
  wait = wait || 1000;

  return function() {
    var now = Date.now();

    if (now - prev > wait) {
      fn.apply(this, arguments);
      prev = now;
    }
  }
}

应用举例:

window.addEventListener('mousemove', throttle(function() {
  console.log(Date.now())
}));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.