Create a simple 10 second countdown

前端 未结 4 1953
一向
一向 2020-11-30 19:48

I would like a line that says:

Your download will begin in (10, 9, 8, etc. Beginning on page load) seconds.

I already have the 10 second downlo

4条回答
  •  借酒劲吻你
    2020-11-30 20:15

    A solution using Promises, includes both progress bar & text countdown.

    ProgressCountdown(10, 'pageBeginCountdown', 'pageBeginCountdownText').then(value => alert(`Page has started: ${value}.`));
    
    function ProgressCountdown(timeleft, bar, text) {
      return new Promise((resolve, reject) => {
        var countdownTimer = setInterval(() => {
          timeleft--;
    
          document.getElementById(bar).value = timeleft;
          document.getElementById(text).textContent = timeleft;
    
          if (timeleft <= 0) {
            clearInterval(countdownTimer);
            resolve(true);
          }
        }, 1000);
      });
    }

    Begining in 10 seconds

提交回复
热议问题