1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <!DOCTYPE html>
<html>
<head>
<title>debounce test</title>
<style>
div {
width: 100px;
height: 100px;
border: 1px solid #000;
}
</style>
</head>
<body>
<p id="result">Move !</p>
<div id="div1">div1</div>
<div id="div2">div2</div>
<script>
var debounce = function(a,b,c,d){return function(){c=this;clearTimeout(d);d=setTimeout(function(){a.apply(c,arguments)},b)}};
document.onmousemove = debounce(function() {
document.getElementById('result').innerHTML = +new Date;
}, 400);
</script>
</body>
</html> |