不活动 5 秒后模态不会触发

modal wont trigger after 5 seconds of inactivity

有人能看出我做错了什么吗?我希望模式在 5 秒不活动后自动弹出,但它似乎不起作用。

这是我的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();
});

使用此代码,它将根据您的要求工作,如果用户在浏览器上没有任何 activity,模式将每 5 秒显示一次

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/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/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    
    
    
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    <a class="btn" 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>