函数写法
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>函数节流和函数防抖</title>
<style>
.demo {
width: 200px;
height: 200px;
border: 1px solid red;
overflow-y: scroll;
margin-top: 50px;
}
.scroll {
height: 5000px;
}
</style>
</head>
<body>
<div class="wrap">
<div id="nothing" class="demo">
普通滚动
<div class="scroll"></div>
</div>
<div id="throttle" class="demo">
函数节流
<div class="scroll"></div>
</div>
<div id="debounce" class="demo">
函数防抖
<div class="scroll"></div>
</div>
</div>
<script type="text/javascript">
// 函数防抖
var debounce = function(idle, action) {
var last;
return function() {
clearTimeout(last);
last = setTimeout(function() {
action();
}, idle);
}
};
// 函数节流
var throttle = function(delay, action) {
var last = 0;
return function() {
var curr = new Date();
if (curr - last > delay) {
action();
last = curr;
}
}
};
// 普通滚动
document.getElementById("nothing").onscroll = function() {
console.log("普通滚动");
};
var test1 = throttle(800, function() {
console.log("函数节流了")});
// 函数节流
document.getElementById("throttle").onscroll = function() {
test1();
};
var test2 = debounce(800, function() {
console.log("函数防抖了")});
// 函数防抖
document.getElementById("debounce").onscroll = function() {
test2();
};
</script>
</body>
</html>
网友回复
如何将linux服务器的文件目录映射到windows电脑磁盘?
Docling 与 MarkItDown 两个库有啥不同?
豆包收费后国产其他ai软件也会跟进收费吗?
JPEG 与 HEIF图片格式区别?
centos7版本太旧无法安装python3.11,如何在docker中运行python3.11?
python如何做个RPA按键精灵的程序?
写一个windows的cmd的python代码如何在命令行中捕获获取复制粘贴的图片?
如何将别人爆款的抖音短视频短剧文案提取为seedance2的提示词?
阿里云域名dns云解析10万次日限额如何应对?
windows电脑如何提交上架ipa苹果应用?


