debounce scroll

by lid0

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<div id="log_c">
    event log (timestamp)<pre id="log">  
</pre>
</div>
<div style="height:5000px"></div>
<div id="top_btn" style='cursor:pointer;position:fixed;bottom:0px;right:0px;background:#cfcfcf;padding:10px'>Top</div>

CSS

#log{    
    background:#cfcfcf;
   
}
#log_c{
         position:fixed;
        top:20px;
    left:0px;
}

JavaScript

last_ts = new Date().getTime();
function s(e){
    
      var now = new Date().getTime(); 
      var diff = (now - last_ts) / 1000; //calc diff
        last_ts = now;
    $("#log").append(now + " +" + diff  + "<br>");
   console.log (new Date().getTime(), e); //debug scroll event
}
s_debounce = _.debounce(s,500);
window.onscroll = s_debounce


//go to top button
$("#top_btn").click( function (){
window.scrollTo(0,0);
});