如何停止计时器并使用javascript函数显示时间

时间:2017-10-22 04:41:04

标签: javascript html

我有一个运行并显示计时器的功能 我有其他功能,我希望它停止计时器,并显示用户输入一些代码的时间

var countDownDate = new Date("October 22, 2017 20:24:00").getTime();

     // Update the count down every 1 second
     var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
    
    // Find the distance between now an the count down date
    var distance = countDownDate - now;
    
    // Time calculations for days, hours, minutes and seconds
   
   
    var sec = (distance/3000000);
	var seconds=sec.toFixed(3)
	
    
    // Output the result in an element with id="timer"
     
	 
	 document.getElementById("timer").innerHTML =  seconds + " ₪" 
    
    
    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("timer").innerHTML = "EXPIRED";
    }
    }, 1000);


      function myFunction(){
	   var b;

      // Get the value of the input field with id="entercode"
      b = document.getElementById("entercode").value;
      if (b==="7b358") {
		clearInterval(x);
        document.getElementById("timer").innerHTML = "timer is stoped";
        document.getElementById("timer").innerHTML = seconds;
      }}
<input id="entercode" type="text" name="firstname" dir="ltr" placeholder="enter code"><input type="button" onClick="myFunction()" value="Submit">
<div id="timer"></div>

为什么第二个功能不起作用?

0 个答案:

没有答案