倒数计时器响应我的命令

时间:2015-11-17 03:51:12

标签: javascript html timer

我正在尝试在JavaScript中制作倒数计时器,特别是我可以设置为一到两分钟,以及启动时的计时器。

这是我从头开始实现的,但我似乎无法让它发挥作用:

var tim = 90
var min = (tim / 60) >> 0;
var sec = tim % 60;
function set1() {
    tim=60;
}
function set2() {
    tim=120;
}
function start() { function{ setInterval(function(){ tim-1; }, 1000);
}
function display() {

document.getElementById("demo").innerHTML = min + ":" + sec ;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="display()">


<p id="demo"></p>
<button onclick="set1()"> set one minute</button>
<button onclick="set2()"> set two minute</button>
<button onclick="start()"> start </button>
</body>
</html>

我也尝试过调整here,中的以下解决方案 无济于事。

function startTimer(duration, display) {
    var start = Date.now(),
        diff,
        minutes,
        seconds;
    function timer() {
        // get the number of seconds that have elapsed since 
        // startTimer() was called
        diff = duration - (((Date.now() - start) / 1000) | 0);

        // does the same job as parseInt truncates the float
        minutes = (diff / 60) | 0;
        seconds = (diff % 60) | 0;

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds; 

        if (diff <= 0) {
            // add one second so that the count down starts at the full duration
            // example 05:00 not 04:59
            start = Date.now() + 1000;
        }
    };
    // we don't want to wait a full second before the timer starts
    timer();
    setInterval(timer, 1000);
}

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};
<body>
    <div>Registration closes in <span id="time"></span> minutes!</div>
</body>

我在这里缺少什么?

3 个答案:

答案 0 :(得分:2)

这是一个解决方案。基本上你只需要抽出一次时间。 每次更新时都需要绘制它。

您在设置间隔中也遇到了一些问题,您再也没有为变量添加过时。

var timeLeft = 90

function set1() {
    timeLeft=60;
    display();
}

function set2() {
    timeLeft=120;
    display();
}

function start() {
   setInterval(function(){ 
       timeLeft = timeLeft -1; 
       display();
   }, 1000);
}

function display() {
    var min = (timeLeft / 60) >> 0;
    var sec = timeLeft % 60;
    document.getElementById("demo").innerHTML = min + ":" + sec ;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="display()">


<p id="demo"></p>
<button onclick="set1()"> set one minute</button>
<button onclick="set2()"> set two minute</button>
<button onclick="start()"> start </button>
</body>
</html>

答案 1 :(得分:1)

更简单的一个..

&#13;
&#13;
var TimerTime;
function startTimer(timerVal){
	clearInterval(TimerTime); //Only if you need to change the time
	var MIN = timerVal/60;
	var SEC = timerVal%60;
	TimerTime = setInterval(function(){
		SEC--;
		if(SEC<0){MIN--;SEC = 59;}
		if(MIN<0){clearInterval(TimerTime)}	
		else{
			document.getElementById("timerDisplay").innerHTML = MIN+" : "+SEC+" remaining...";
		}
	},1000);
}
window.onload = startTimer(60);
&#13;
<select onchange="startTimer(this.value)">
  <option value="">Select Time</option>
  <option value="60">1 Minute</option>
  <option value="120">2 Minuts</option>
</select>
<div id="timerDisplay"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

好吧,因为你试图在另一篇文章中修改代码中的解决方案,我也可以告诉你如何做到这一点。

let spacePreserveAttribute = NSXMLNode.attributeWithName("xml:space", stringValue: "preserve") as! NSXMLNode
// ...
if let tElements = try? graf.nodesForXPath("descendant::w:t") as! [NSXMLElement] {
    for t in tElements {
        var tAttrs: [NSXMLNode] = t.attributes ?? []
        tAttrs.append(
            spacePreserveAttribute.copy() as! NSXMLNode
        )
        t.attributes = tAttrs
    }
}
function startTimer(duration, display) {
  var start = Date.now(), diff;

  (function timer() {
    diff = duration - (((Date.now() - start) / 1000) | 0);

    if (diff >= 0) {
      display(diff);
      setTimeout(timer, 1000);
    }
  }());
}

window.onload = function() {
  var element = document.querySelector('#time'),
      started = false;

  function display(diff) {
    var minutes = (diff / 60) | 0,
        seconds = (diff % 60) | 0;
    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;
    element.innerHTML = minutes + ":" + seconds;
  }

  function handleClick(duration) {
    return function() {
      if (!started) {
        started = true;
        startTimer(duration, display);
      }
    };
  }

  function attachHandler(id, duration) {
    var ftn = handleClick(duration);
    document.getElementById(id).addEventListener('click', ftn);
  }

  attachHandler('one-minute', 60);
  attachHandler('two-minute', 120);
};