JS 手写防抖
<!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" id="ipt">
<script>
var oIpt = document.getElementById("ipt");
function getData(e) {
console.log("ajax请求")
console.log(this);
console.log(e)
}
oIpt.oninput = debounce(getData, 1300);
function debounce(fn, time) {
var timer = null;
return function () {
clearTimeout(timer);
var _this = this;
var arg = arguments[0];
timer = setTimeout(function () {
fn.call(_this, arg);
}, time)
}
}
</script>
</body>
</html>