$(function () {
// document.getElementById('submit').addEventListener('click', debounce(submitFn));
document.getElementById('submit').addEventListener('click', submitFn.debounce(1));
})
function submitFn (val) {
console.log( $('#inputVal').val() );
console.log(this);
console.log(val);
}
Function.prototype.debounce = function () {
const Fn = this;
let flag = null;
const args = arguments;
return function () {
let firstRun = !flag;
if (flag) { clearTimeout(flag) };
if (firstRun) {
Fn.apply(this, args)
}
flag = setTimeout(() => {
flag = null;
}, 1000)
}
}
const debounce = function (Fn) {
let flag = null;
return function () {
let firstRun = !flag;
if (flag) { clearTimeout(flag) };
if (firstRun) {
Fn.apply(this, arguments)
}
flag = setTimeout(() => {
flag = null;
}, 500)
}
}
网友评论