// function throttling var canRun = true ; document. getElementById( "throttle"). onscroll = function ( ) { if( !canRun) { // determine whether it is idle, if it is executing, return directly return ; } canRun = false ; setTimeout( function ( ) { console. log( "Function throttling") ; canRun = true ; }, 300) ; };
// function debounce var timer = false ; document. getElementById( "debounce"). onscroll = function ( ) { clearTimeout(timer) ; // clear unexecuted code, reset back to initialization state timer = setTimeout( function ( ) { console. log( "Function debounce") ; }, 300) ; };