这段函数为什么不执行?
return function() {
var context = this;
var args = arguments;
var now = Date.now();
console.log('now == ', now)
if (now - prev >= delay) {
func.apply(context, args);
prev = Date.now();
}
}
网页所有代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<div class="test01">点击</div>
</div>
</body>
<script>
// 节流throttle代码(定时器):
var throttle = function(func, delay) {
var prev = Date.now();
console.log('prev == ', prev)
return function() {
var context = this;
var args = arguments;
var now = Date.now();
console.log('now == ', now)
if (now - prev >= delay) {
func.apply(context, args);
prev = Date.now();
}
}
}
function handle() {
console.log(Math.random());
}
document.querySelector('.test01').onclick = function() {
throttle(handle, 5 * 1000)
}
</script>
</html>