简体   繁体   English

单击时隐藏按钮,计时器用尽时显示按钮

[英]Hide Button When Clicked, Show Button When Timer Runs Out

I currently have a timer , that counts down from 2 minutes. 我目前有一个计时器,从2分钟开始倒数。 what I would like to happen is when the button is clicked, it is hidden until the timer runs out and when the timer runs out it is visible/clickable again. 我想发生的是单击按钮时,它一直隐藏到计时器用完为止,而当计时器用尽时又可以看到/单击。 I would also like the timer to be hidden until the button is clicked, to be visible when the button is clicked and then to be hidden once the timer runs out. 我还希望在单击按钮之前隐藏计时器,在单击按钮时显示该计时器,然后在计时器用尽时将其隐藏。

here is my code 这是我的代码

js JS

function startTimer() {
userInput = 120;
if(userInput.length == 0){
    alert("Please enter a value");
} else {
var numericExpression = /^[0-9]+$/;

function display( notifier, str ) {
document.getElementById(notifier).innerHTML = str;
}

function toMinuteAndSecond( x ) {
return Math.floor(x/60) + ":" + x%60;
}

function setTimer( remain, actions ) {
(function countdown() {
   display("countdown", toMinuteAndSecond(remain));         
   actions[remain] && actions[remain]();
   (remain -= 1) >= 0 && setTimeout(countdown, 1000);
})();
}

setTimer(userInput, {
 0: function () { alert( "Time Is Up. Please Sumbit Vote.");       }
}); 

}
}

html html

<div id="countdown"></div>
<input type="button" onclick="startTimer()" value="Start Timer"> 

fiddle http://jsfiddle.net/grahamwalsh/qur9r3d8/ 小提琴http://jsfiddle.net/grahamwalsh/qur9r3d8/

You can hide and unhide the button using JS 您可以使用JS隐藏和取消隐藏按钮

JSFiddle 的jsfiddle

Add an ID to your button 向您的按钮添加ID

<input id="btn" type="button" onclick="startTimer()" value="Start Timer"/>

JScode JS代码

function startTimer() {
//hide button
document.getElementById("btn").style.display = "none";
//un-hide timer
document.getElementById("countdown").style.display = "inline";

userInput = 10;
if (userInput.length == 0) {
    alert("Please enter a value");
} else {
    var numericExpression = /^[0-9]+$/;

    function display(notifier, str) {
        document.getElementById(notifier).innerHTML = str;
    }

    function toMinuteAndSecond(x) {
        return Math.floor(x / 60) + ":" + x % 60;
    }

    function setTimer(remain, actions) {
        (function countdown() {
            display("countdown", toMinuteAndSecond(remain));
            actions[remain] && actions[remain]();
            (remain -= 1) >= 0 && setTimeout(countdown, 1000);
        })();
    }

    setTimer(userInput, {
        0: function () {
            alert("Time Is Up. Please Sumbit Vote.");
            //un-hide button
             document.getElementById("btn").style.display = "inline";
           //hide timer
            document.getElementById("countdown").style.display = "none";
        }
    });
  }
 }

Here is a fiddle with the solution: 这是解决方案的小提琴:

Use the display property: 使用显示属性:

document.getElementById("button1").style.display="none";

and to show: 并显示:

document.getElementById("button1").style.display="block";

fiddle 小提琴

Make sure to add button1 as an id to your button: 确保将button1作为ID添加到您的按钮:

<input id="button1" type="button" onclick="startTimer()"

The fiddle shows where you should put this code... 小提琴显示您应该在哪里放置此代码...

I went ahead and built it from scratch using JQuery as your friend suggested. 我按照您的朋友的建议使用JQuery从头开始构建它。 I think all the answers here using your setTimeout are taking the wrong approach. 我认为所有使用setTimeout的答案都采用了错误的方法。 This is more of a job for setInterval which will provide slightly less performance overhead and much cleaner code. 对于setInterval ,这更多的是工作,它将提供更少的性能开销和更简洁的代码。

Working Example: http://codepen.io/Chevex/pen/RNomGG 工作示例: http : //codepen.io/Chevex/pen/RNomGG

First, some simple HTML to work with. 首先,使用一些简单的HTML。

<div id="timerDisplay"></div>
<button id="startTimer">Start Timer</button>

Next, a simple timer script. 接下来,一个简单的计时器脚本。

// Passing a function to $() is the same as $(document).on('ready', function () { ... });
// It waits for the entire page to be loaded before running the function, which is usually what you want.
$(function () {
  // Get reference to our HTML elements and store them as variables.
  // I prepend them with dollar signs to signify they represent HTML elements.
  var $startTimer = $('#startTimer');
  var $timerDisplay = $('#timerDisplay');

  // The initial time of the timer.
  var time = 120;

  // Hide the timer display for now, until the button is clicked.
  $timerDisplay.hide();

  // Set up a click handler on our $startTimer button.
  $startTimer.click(function () {
    // When the button is clicked, do the following:

    // Set the disabled property to true for our button.
    // Effectively the same as <button id="startTimer" disabled>Start Timer</button>
    $startTimer.prop('disabled', true);

    // Fade in our timer display DIV element.
    $timerDisplay.fadeIn();

    // Set a timeRemaining variable to the value of the initial time.
    var timeRemaining = time;

    // Declare an interval function that runs every second.
    // Also get reference to the intervalId that it returns so we can kill it later.
    var intervalId = setInterval(function () {
      // Every time the interval runs (every second), do the following: 

      // Create a formatted countdown timestamp using the timeRemaining.
      var timeStamp = Math.floor(timeRemaining/60) + ':' + timeRemaining%60;

      // Set the text of our timer display DIV element to our formatted timestamp.
      $timerDisplay.text(timeStamp);

      // If the timeRemaining is zero, clean up.
      if (timeRemaining === 0) {
        // Kill the interval function so it doesn't run again.
        clearInterval(intervalId);
        // Fade out our timer display DIV element.
        $timerDisplay.fadeOut();
        // Show the alert informing the user the timer is up.
        alert('Time is up, please submit a vote :)');
        // Re-enable the startTimer button.
        $startTimer.prop('disabled', false);
      }
      // Otherwise subtract one second from the timeRemaining and allow the interval to continue.
      else {
        timeRemaining--;
      }
    }, 1000);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM