countdowntimer - 1 minutes 30 second countdown timer javascript - Stack Overflow

I have the code but it's for 2 minutes timer andi need it to modify to 1 minutes 30 seconds time

I have the code but it's for 2 minutes timer and i need it to modify to 1 minutes 30 seconds timer.

I have tried but fails to start timer from 1:30.

As i'm beginner in this line and want to learn how to do that.

here it's the code

<div id=timer></div>
<script type="text/javascript">
  var timeoutHandle;

  function countdown(minutes, seconds) {
    var seconds = 60;
    var mins = minutes

    function tick() {
      var counter = document.getElementById("timer");
      var current_minutes = mins - 1
      seconds--;
      counter.innerHTML =
        current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
      if (seconds > 0) {
        timeoutHandle = setTimeout(tick, 1000);
      } else {

        if (mins > 1) {

          // countdown(mins-1);   never reach “00″ issue solved:Contributed by Victor Streithorst
          setTimeout(function() {
            countdown(mins - 1);
          }, 1000);

        }
      }
    }
    tick();
  }

  countdown(2);
</script>

I have the code but it's for 2 minutes timer and i need it to modify to 1 minutes 30 seconds timer.

I have tried but fails to start timer from 1:30.

As i'm beginner in this line and want to learn how to do that.

here it's the code

<div id=timer></div>
<script type="text/javascript">
  var timeoutHandle;

  function countdown(minutes, seconds) {
    var seconds = 60;
    var mins = minutes

    function tick() {
      var counter = document.getElementById("timer");
      var current_minutes = mins - 1
      seconds--;
      counter.innerHTML =
        current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
      if (seconds > 0) {
        timeoutHandle = setTimeout(tick, 1000);
      } else {

        if (mins > 1) {

          // countdown(mins-1);   never reach “00″ issue solved:Contributed by Victor Streithorst
          setTimeout(function() {
            countdown(mins - 1);
          }, 1000);

        }
      }
    }
    tick();
  }

  countdown(2);
</script>

Share Improve this question edited Sep 28, 2018 at 3:03 Ram 145k16 gold badges172 silver badges200 bronze badges asked Sep 28, 2018 at 2:51 Sahil Kumar GabaSahil Kumar Gaba 211 gold badge1 silver badge5 bronze badges 6
  • countdown(1, 30), plus remove the var seconds = 60. – Obsidian Age Commented Sep 28, 2018 at 2:53
  • @MattWay and remove var seconds = 60; – Jaromanda X Commented Sep 28, 2018 at 2:53
  • 1 or replace it with seconds = seconds || 0 – Jaromanda X Commented Sep 28, 2018 at 2:54
  • Where is minutes ing from? – CodeSpent Commented Sep 28, 2018 at 3:03
  • I have changed now the timer starts only from 30 seconds – Sahil Kumar Gaba Commented Sep 28, 2018 at 3:07
 |  Show 1 more ment

3 Answers 3

Reset to default 1

<div id=timer></div>
<script type="text/javascript">
    var timeoutHandle;
    function countdown(minutes, seconds) {
        function tick() {
            var counter = document.getElementById("timer");
            counter.innerHTML =
                minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
            seconds--;
            if (seconds >= 0) {
                timeoutHandle = setTimeout(tick, 1000);
            } else {
                if (minutes >= 1) {
                    // countdown(mins-1);   never reach “00″ issue solved:Contributed by Victor Streithorst
                    setTimeout(function () {
                        countdown(minutes - 1, 59);
                    }, 1000);
                }
            }
        }
        tick();
    }

    countdown(1, 30);
</script>

I will use something like this:

<div id=timer></div>
<script type="text/javascript">

    var maxTicks = 90;
    var tickCount = 0;
    
    var tick = function()
    {
        if (tickCount >= maxTicks)
        {
            // Stops the interval.
            clearInterval(myInterval);
            return;
        }
    
        /* The particular code you want to excute on each tick */
        document.getElementById("timer").innerHTML = (maxTicks - tickCount);
    
        tickCount++;
    };
    
    // Start calling tick function every 1 second.
    var myInterval = setInterval(tick, 1000);

</script>

detailed options https://www.growthsnippets./30-second-countdown-timer-javascript/

var remainingTime = 30;
    var elem = document.getElementById('countdown_div');
    var timer = setInterval(countdown, 1000); //set the countdown to every second
    function countdown() {
      if (remainingTime == -1) {
        clearTimeout(timer);
        doSomething();
      } else {
        elem.innerHTML = remainingTime + ' left';
        remainingTime--; //we subtract the second each iteration
      }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744357949a4570325.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信