移动鼠标触发
//防抖(非立即执行)
function debounce_1(fn,wait){
var timerId = null;
return function(){
var context = this
var args = arguments
clearTimeout(timerId);
timerId = setTimeout(() => {
fn.apply(context,args)
},wait)
}
}
//防抖(立即执行)
function debounce_2(fn,wait){
var timerId = null;
var flag = true;
return function(){
var context = this
var args = arguments
clearTimeout(timerId);
if(flag){
fn.apply(context, args);
flag = false
}
timerId = setTimeout(() => { flag = true},wait)
}
}
//防抖(合并版)
function debounce_merge(fn,wait = 500,isImmediate = false){
var timerId = null;
var flag = true;
return function(){
var context = this
var args = arguments
clearTimeout(timerId )
if(isImmediate ){
if(flag){
fn.apply(context,args)
flag = false
}
timerId = setTimeout(function(){
flag = true
},wait)
}else{
timerId = setTimeout(function(){
fn.apply(context,args)
},wait)
}
}
}
//定义函数
//节流(非立即执行)
function throttle_1(fn,wait){
var flag = true;
return function(){
var context = this
var args = arguments
if(flag){
flag = false
setTimeout(() => {
fn.apply(context,args)
flag = true
},wait)
}
}
}
//节流(立即执行)
function throttle_2(fn,wait){
var flag = true;
return function(){
var context = this
var args = arguments
if(flag) {
fn.apply(context,args);
flag = false;
setTimeout(() => {
flag = true
},wait)
}
}
}
//节流(合并)
function throttle_merge(fn,wait = 500,isImmediate = false){
var flag = true;
return function(){
if(flag == true){
var context = this
var args = arguments
flag = false
isImmediate && fn.apply(context,args)
setTimeout(() => {
!isImmediate && fn.apply(context,args)
flag = true
},wait)
}
}
}
setTimeout
来实现的,也可以使用 Date.now()
来实现,思路一样,偷个懒就不写了,嘿嘿;debounce(fn,wait,isImmediate)
throttle(fn,wait,isImmediate)
截图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body{height: 1200px;}
div{width: 100%;background-color: lightcyan;overflow: hidden;}
p{float: left;width: 10px;height:10px;background-color: pink;margin: 1px;}
</style>
</head>
<body>
防抖(非立即执行) <div id="d1_1"></div>
防抖(立即执行) <div id="d1_2"></div>
节流(非立即执行) <div id="d2_1"></div>
节流(立即执行) <div id="d2_2"></div>
<!-- 原生 <div id="d3"></div> -->
</body>
<script>
//防抖(非立即执行)
function debounce_1(fn,wait){
var timerId = null;
return function(){
clearTimeout(timerId);
timerId = setTimeout(() => {
fn.apply(this,arguments)
},wait)
}
}
//防抖(立即执行)
function debounce_2(fn,wait){
var timerId = null;
var flag = true;
return function(){
clearTimeout(timerId);
if(flag){
fn.apply(this,arguments);
flag = false
}
timerId = setTimeout(() => { flag = true},wait)
}
}
//节流(非立即执行)
function throttle_1(fn,wait){
var flag = true;
return function(){
if(flag == true){
flag = false
var timer = setTimeout(() => {
fn.apply(this,arguments)
flag = true
},wait)
}
}
}
//节流(立即执行)
function throttle_2(fn,wait){
var flag = true;
var timer = null;
return function(){
if(flag) {
fn.apply(this,arguments);
flag = false;
timer = setTimeout(() => {
flag = true
},wait)
}
}
}
//定义事件函数
function debounce_fn1(){ addElement(d1_1) }
function debounce_fn2(){ addElement(d1_2) }
function throttle_fn1(){ addElement(d2_1) }
function throttle_fn2(){ addElement(d2_2) }
// function origin(){ addElement(d3) }
//注册事件
var body = document.getElementsByTagName('body')[0]
body.addEventListener("mousemove",debounce_1(debounce_fn1,500))
body.addEventListener("mousemove",debounce_2(debounce_fn2,500))
body.addEventListener("mousemove",throttle_1(throttle_fn1,500))
body.addEventListener("mousemove",throttle_2(throttle_fn2,500))
// body.addEventListener("mousemove",origin)
//辅助函数
function addElement(f){
var node=document.createElement("p");
f.appendChild(node);
}
</script>
</html>