Home > Software engineering >  modal wont trigger after 5 seconds of inactivity
modal wont trigger after 5 seconds of inactivity

Time:05-04

could someone see what i am doing wrong here? I wish for the modal to auto popup after 5 seconds of inactivity but it just dont seem to work.

here is my jq

var THRESHOLD = 5 * 1000;
var lastActive = Date.now();
$(window).ready(function() {
    if (Date.now() > lastActive   THRESHOLD) {
     $('#modal').fadeIn(1500).css('display','block').trigger('click');
    }
    lastActive = Date.now();
});

CodePudding user response:

Use this code it will work according to your requirement modal will show every 5 second if a user has not any activity on browser

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA 058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5 76PVCmYl" crossorigin="anonymous"></script>
    
    
    
    <div  id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
      <div  role="document">
        <div >
          <div >
            <h5  id="exampleModalLongTitle">Modal title</h5>
            <button type="button"  data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div >
            ...
          </div>
          <div >
            <button type="button"  data-dismiss="modal">Close</button>
            <button type="button" >Save changes</button>
          </div>
        </div>
      </div>
    </div>
    <a  data-toggle="modal" href="#myModal">Launch Modal</a>
    
    <script type="text/javascript">
        $(document).ready(function () {
            var inactiveTime;
            $('*').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () {
    
                function alertUser() {
                     $('#myModal').modal('show');
                    //alert("User is inactive.");
                }
                
                clearTimeout(inactiveTime);
    
                inactiveTime = setTimeout(alertUser, 1000 * 5); // 5 seconds
            });
            $("body").trigger("mousemove");
        });
    </script>

  • Related