<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>防抖和节流</title>
</head>
<body>
<button id="jieliu">0</button>
<button id="fangdou" style="margin-left: 20px;">0</button><br/>
<button onclick="throttle(1000)">节流</button>
<button onclick="debounce(1000)">防抖</button>
</body>
</html>
<script>
let timer;
//节流
let jl = document.getElementById("jieliu")
let canRun = true;
const throttle=(time)=>{
if (!canRun) return;
canRun = false;
setTimeout(()=>{
jl.innerHTML++
canRun=true
节流和防抖
于 2022-03-04 11:18:28 首次发布