显示时重置倒数计时器 - Javascript / 模态

Resetting a Countdown Timer When It Is Shown - Javascript / Modal

包括片段在内的一切都正常工作,但我的 30 秒倒计时计时器在显示时不会重置为 00:30...它一遍又一遍地连续 运行。

$(document).ready(initApp);

function initApp() {
  console.debug('initApp');
  const activityModal = $('#modal101');
  activityModal.hide();
  const activityMaxDelayWarning = 3 * 1000; //  (display message at 3 seconds of inactivity) 
  const activityMaxDelay = 60 * 1000; //  (total time to logout) 60s = 1min
  const activityCheckInterval = 5 * 1000; 

  activityRegister();
  document.addEventListener('mousemove', activityRegister); // look for mouse movement
  document.addEventListener("scroll", activityRegister); // look for scrolling
  setInterval(activityCheck, activityCheckInterval);

  function activityRegister() {
    popupHide();
    activityLast = Date.now();
    console.debug('Activity set to', activityLast);
  }

  function userIsLogguedIn() {
    //DO it
    return true;
  }

  function activityCheck() {
    const delay = Date.now() - activityLast;
    console.debug("Activity delay is", delay);

    if (!userIsLogguedIn()) {
      console.debug("     user is not logged in");
      return;
    }
    if (delay > activityMaxDelay) {
      console.debug("     will logout");
      logout();
    } else if (delay > activityMaxDelayWarning) {
      console.debug("     will show warning");
      popupShow();
    }
  }

  function popupShow() {
    activityModal.show();
  }

  function popupHide() {
    activityModal.hide();
  }

  function logout() {
    window.location.href = 'https://whosebug.com';
  }
}

function startTimer(duration, display) {
  var timer = duration,
    minutes, seconds;
  setInterval(function() {
    minutes = parseInt(timer / 60, 10)
    seconds = parseInt(timer % 60, 10);

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (--timer < 0) {
      timer = duration;
    }
  }, 1000);
}

function resetTimer() {
  timer = 30 * 1;
}

window.onload = function() {
  var fiveMinutes = 30 * 1,
    display = document.querySelector('#time');
  startTimer(fiveMinutes, display);
};
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>



  <div>
    <h3>Dont move and a message will appear...</h3>
  </div>



  <div id="modal101" class="modal" tabindex="-1" role="dialog" style="background-color: rgba(255, 0, 0, 0.88);">
    <div class="modal-dialog" role="document">
      <div class="modal-content centered" style="max-width:550px;background-color:#323234;color:white;">
        <div class="modal-header">
          <h1 class="modal-title" style="text-align: center;">Warning!!!!</h1>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span id="time" style="color:white;">00:30</span>
          </button>
        </div>
        <div class="modal-body">
          <p>You will be redirected due to inactivity in 30 seconds</p>
        </div>
      </div>
    </div>
  </div>

谁能帮我做到这一点 每次都从 00:30 秒开始 它显示在模式中,所以无论如何它总是从 30 秒开始:)

首先,代码很杂乱,我不知道你到底要做什么。 与@Merky 直接讨论,我能够理解一些。

这是我推测的解决方案。需要重新触摸,但这将解决根本问题。

<script>
$(document).ready(initApp);

function initApp() {
  console.debug('initApp');
  const activityModal = $('#modal101');
  activityModal.hide();
  const activityMaxDelayWarning = 3 * 1000; //  (display message at 3 seconds of inactivity) 
  const activityMaxDelay = 60 * 1000; //  (total time to logout) 60s = 1min
  const activityCheckInterval = 5 * 1000; 

  activityRegister();
  document.addEventListener('mousemove', activityRegister); // look for mouse movement
  document.addEventListener("scroll", activityRegister); // look for scrolling
  setInterval(activityCheck, activityCheckInterval);

  function activityRegister() {
    popupHide();
    activityLast = Date.now();
    console.debug('Activity set to', activityLast);
  }

  function userIsLogguedIn() {
    //DO it
    return true;
  }

  function activityCheck() {
    const delay = Date.now() - activityLast;
    console.debug("Activity delay is", delay);

    if (!userIsLogguedIn()) {
      console.debug("     user is not logged in");
      return;
    }
    if (delay > activityMaxDelay) {
      console.debug("     will logout");
      logout();
    } else if (delay > activityMaxDelayWarning) {
      console.debug("     will show warning");
      popupShow();
    }
  }

  function popupShow() {
    if(!activityModal.is(':visible')) {
        resetTimer();
        activityModal.show();
    }
  }

  function popupHide() {
    activityModal.hide();
  }

  function logout() {
    // don't do this here.
    //window.location.href = 'https://whosebug.com';
  }
}

function updateTimerDiv(display) {
    var minutes = parseInt(window.timer_val / 60, 10)
    var seconds = parseInt(window.timer_val % 60, 10);

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;
}

function startTimer(duration, display) {
  window.timer_val = duration;
  var minutes, seconds;
  setInterval(function() {
    updateTimerDiv(display);    
    if (--window.timer_val < 0) {
        window.timer_val = duration;
    }
  }, 1000);
}

function resetTimer() {
  console.log("reset timer");
  window.timer_val = 30 * 1;
  updateTimerDiv(document.querySelector('#time'));
}

window.onload = function() {
  var fiveMinutes = 30 * 1,
    display = document.querySelector('#time');
  startTimer(fiveMinutes, display);
};
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>



  <div>
    <h3>Dont move and a message will appear...</h3>
  </div>



  <div id="modal101" class="modal" tabindex="-1" role="dialog" style="background-color: rgba(255, 0, 0, 0.88);">
    <div class="modal-dialog" role="document">
      <div class="modal-content centered" style="max-width:550px;background-color:#323234;color:white;">
        <div class="modal-header">
          <h1 class="modal-title" style="text-align: center;">Warning!!!!</h1>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span id="time" style="color:white;">00:30</span>
          </button>
        </div>
        <div class="modal-body">
          <p>You will be redirected due to inactivity in 30 seconds</p>
        </div>
      </div>
    </div>
  </div>