throttle vs debounce

by ph822720355

HTML

<b>Mousemove Event(請滑動鼠標觀察數值狀況)</b>
<br>一般狀況: <input type='text' id='output1' disabled>(不斷更新)

<br>Throttle(節流) <input type='text' id='output2' disabled>(每1秒更新1次)

<br>Debounce(去抖動) <input type='text' id='output3' disabled>(1秒內沒移動才更新)


<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js'></script>
<script>
$(window).mousemove( function(e) {
	$('#output1').val( e.clientX + ', ' + e.clientY )
})
$(window).mousemove( _.throttle( function(e) {
	$('#output2').val( e.clientX + ', ' + e.clientY )
}, 1000))
$(window).mousemove( _.debounce( function(e) {
	$('#output3').val( e.clientX + ', ' + e.clientY )
}, 1000))
</script>

CSS

body {
  background: lightgrey;
}

input {
  color: red;
}