// var button = document.getElementById('save')
// button.onclick = function () {
// var throttleFun = throttle(1000, fun)
// throttleFun()
// }
// function fun() {
// console.log('Hello')
// }
// function throttle(delay, callback) {
// var timer
// return function () {
// if (!timer) {
// timer = setTimeout(function () {
// callback()
// timer = null
// }, delay)
// }
// }
// }
var save = document.getElementById('save')
var throttleFun = throttle(1000, fun)
save.onclick = function () {
throttleFun()
}
function fun () {
console.log('保存')
}
function throttle (delay,callback) {
var timer;
return function () {
if (!timer) {
timer = setTimeout(function () {
callback()
timer = null
}, delay)
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=, initial-scale=">
<meta http-equiv="X-UA-Compatible" content="">
<title>节流函数的理解</title>
</head>
<body>
<button id='save'>保存</button>
</body>
</html>