1 Star 0 Fork 0

Shadow / code-snippet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
throttle.html 788 Bytes
一键复制 编辑 原始数据 按行查看 历史
Shadow 提交于 2022-04-21 22:29 . 手写promise
<!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>
<input type="text" oninput="onInput()">
<script>
const onInput = throttle(triggerSearch, 500);
function triggerSearch() {
console.log('search');
}
function throttle(fn, interval) {
let lastTime;
return function (...args) {
if(lastTime) {
const now = Date.now();
if((now - lastTime) > interval) {
lastTime = now;
fn(...args);
}
} else {
lastTime = Date.now();
fn(...args);
}
}
}
</script>
</body>
</html>
1
https://gitee.com/ytiona/code-snippet.git
git@gitee.com:ytiona/code-snippet.git
ytiona
code-snippet
code-snippet
master

搜索帮助