当计数器达到零时启用按钮 [英] Enable button when counter reaches zero

查看:83
本文介绍了当计数器达到零时启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个禁用的按钮,我想在其中放置一个计数器,我想要做的是当计数器达到零时启用它,我该怎么做?在下面的代码中,计数器没有出现在按钮内部,我不想重置按钮我只想在按钮启用时达到零,这是我到目前为止所尝试的:

I have a button its disabled and i want to put a counter inside it, what i want to do is when the counter reaches zero it get enabled, how can i do that? in the code below the counter doesn't appear inside the button and i don't want the reset button i just want the button to be enabled when it reaches zero, here is what i have tried so far:

function Countdown()
{
    this.start_time = "00:30";
    this.target_id = "#timer";
    this.name = "timer";
    this.reset_btn = "#reset";
}

Countdown.prototype.init = function()
{
this.reset();
setInterval(this.name + '.tick()',1000)
}

Countdown.prototype.reset = function()
{
    $(this.reset_btn).hide();
    time = this.start_time.split(":");
    //this.minutes = parseInt(time[0]);
    this.seconds = parseInt(time[1]);
    this.update_target();
}

Countdown.prototype.tick = function()
{
    if(this.seconds > 0) //|| this.minutes > 0)
    {
        if(this.seconds == 0)
        {
           // this.minutes = this.minutes - 1;
            this.seconds = 59
        } else {
        this.seconds = this.seconds - 1;
        }
    }
    this.update_target()
}

Countdown.prototype.update_target = function()
{
    seconds = this.seconds;
    if (seconds == 0) $(this.reset_btn).show();
    else if(seconds < 10) seconds = "0" + seconds;
    $(this.target_id).val(this.seconds)
}

timer = new Countdown();
timer.init();

$(document).ready(function(){
    $("#reset").click(function(){
        //timer = new Countdown();  
        timer.reset();
    });
});

.hidden {
    display: none;
}

<button type="text" id="timer" disabled>Counter should be inside me, and enable me when it reaches 0</button>
         <button id="reset">Reset</button>

推荐答案

这比你得到的要简单得多。只需使用 window.setTimeout ()

This is much simpler than what you've got. Just use window.setTimeout().

请注意,在浏览器中,追踪高精度的时间并不是非常可靠。您可能需要查看 moment.js 或使用 performance.now() ,以便更轻松地处理API。

Keep in mind that tracking time to a high precision is not super reliable in a browser. You may want to look at moment.js or use performance.now() for an easier API to handle that.

// Get refreence to span and button
var spn = document.getElementById("count");
var btn = document.getElementById("btnCounter");

var count = 5;     // Set count
var timer = null;  // For referencing the timer

(function countDown(){
  // Display counter and start counting down
  spn.textContent = count;
  
  // Run the function again every second if the count is not zero
  if(count !== 0){
    timer = setTimeout(countDown, 1000);
    count--; // decrease the timer
  } else {
    // Enable the button
    btn.removeAttribute("disabled");
  }
}());

<button id="btnCounter" disabled>Time left: <span id="count"></span></button>

这篇关于当计数器达到零时启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆