0

basically I want to run some code in jquery after the user has stopped doing anything in the browser like click,scroll. How to know if all functions have finished and jquery is not being busy (being run or used)

Basically run code only when the user is idle

Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76

4 Answers4

1

If I am getting you right, this is what you were looking for

<script>
   $(function(){
     (function(seconds) {
     var refresh,       
      intvrefresh = function() {
      clearInterval(refresh);
      refresh = setTimeout(function() {
         alert('No activity from user 10 seconds, put your code here !');
       }, seconds * 1000);
      };
     $(document).on('click keydown keyup mousemove', function() { intvrefresh() });
                    intvrefresh();
      }(10));  
    });
 </script>
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
0

Typically you'd wait until everything has loaded. This means that all elements have finished loading on the page (e.g. Images loaded in). There's nothing here to tell you that there's no JavaScript running in the background though.

$(window).load(function() {
   // Do stuff
});
Ian
  • 33,605
  • 26
  • 118
  • 198
0
try {
       var str =$('body').html();
    } catch ( e ) {
       alert('jquery not used');
    }

Do something like this. jsfiddle

moyeen52
  • 425
  • 3
  • 13
0

I just answered it here. See if it makes sense.

TLDR;

You can do it more elegantly with underscore and jquery-

$('body').on("click mousemove keyup", _.debounce(function(){
    // your code here.
}, 1200000)) // 20 minutes debounce
Community
  • 1
  • 1
Kruttik
  • 404
  • 3
  • 7