1

I want to dismiss bootstrap modal if no action in browser screen for 10-15 seconds.

I have tried settimeout() function but this will not check the action in the browser.

setTimeout(function() {$('#form').modal('hide');}, 10000);

So, Is there any way to hide modal box if no action in the browser?

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104

1 Answers1

1

The snippet below with set the flag actionAppeared if there a keypress in the keyboard or 'mousemove' in the mouse.

var actionAppeared = false;

jQuery(document).mousemove(function (e) { actionAppeared = true; });
jQuery(document).keypress(function (e) { actionAppeared = true; });

setTimeout(function() {
    if(!actionAppeared) {
        $('#form').modal('hide');}
    }
, 10000);

Here is a working demo. Open the console to see the mousemove and keypress events.

The mousemove events is triggered really easy so to test it open the modal and quickly move the cursor away from the keyboard.

Tasos K.
  • 7,979
  • 7
  • 39
  • 63