当前位置:网站首页>js函数防抖和函数节流及其使用场景
js函数防抖和函数节流及其使用场景
2022-08-02 08:23:00 【是小飞呀嘿】
目录
一、函数防抖
使用场景:【永远在执行最后一次】 回到顶部,立即(实时)搜索
代码段
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
<h1>函数防抖</h1>
</body>
</html> //函数防抖使用场景:【永远在执行最后一次】 回到顶部,立即(实时)搜索
// debounce() 函数进入页面立即执行
// debounce() 必须返回函数 事件只能调用函数
//防抖函数
function debounce(callback,time = 300){
let t;
return function(){
clearTimeout(t);
t = setTimeout(callback,time);
}
}
//绑定滚动条事件
window.onscroll = debounce(function(){
console.log("调用了1次");
},500);
效果图
二、函数节流
使用场景:所有的数据请求【按钮】 下拉刷新
代码段
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
<h1>函数节流</h1>
</body>
</html> // 所有的数据请求【按钮】 下拉刷新
// 函数节流
function throlle(callback,time){
let lasttime = new Date().getTime();
return function(){
let now = new Date().getTime();
if(now - lasttime > time){
callback();
lasttime = now;
}
}
}
/*
window.onscroll = function(){
console.log("调用了1次");
} */
window.onscroll = throlle(function(){
console.log("调用了1次");
},1500);效果图

边栏推荐
猜你喜欢

EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network

Biotin hydrazide HCl|CAS:66640-86-6|Biotin-hydrazide hydrochloride

mysql 中 in 的用法

测试时大量TIME_WAIT

UVM事务级建模

Stop mental exhaustion Daily sharing

ip地址那点事(二)

Biotin-C6-amine|N-biotinyl-1,6-hexanediamine|CAS: 65953-56-2

postman下载安装汉化及使用
主流监控系统工具选型及落地场景参考
随机推荐
Three types of [OC learning notes] Block
深度学习汇报(4)
Biotin - LC - Hydrazide | CAS: 109276-34-8 | Biotin - LC - Hydrazide
Redis分布式锁
mysqldump --set-gtid-purged=OFF
Redisson distributed lock source code analysis for high-level use of redis
Business Intelligence Platform BI Business Intelligence Analysis Platform How to Choose the Right Business Intelligence Platform BI
LeetCode_2357_使数组种所有元素都等于零
location对象,navigator对象,history对象学习
How Engineers Treat Open Source --- A veteran engineer's heartfelt words
R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re
R language plotly visualization: use the plotly visualization model to predict the true positive rate (True positive) TPR and false positive rate (False positive) FPR curve under different thresholds
Flink 监控指南 被动拉取 Rest API
工程师如何对待开源 --- 一个老工程师的肺腑之言
MySQL 中 count() 和 count(1) 有什么区别?哪个性能最好?
在 QT Creator 上配置 opencv 环境的一些认识和注意点
近期在SLAM建图和定位方面的进展
pycharm的基本使用教程(1)
Pycharm (1) the basic use of tutorial
TiFlash 存储层概览