暂停倒数计时器(JavaScript)

时间:2016-09-06 09:08:46

标签: javascript jquery timer

我希望倒计时器在我单击暂停按钮时暂停,并在我单击go按钮时从它停止的位置开始。如果单击重置时再次启用go按钮也会很好,但是到目前为止,只要设置了定时器,它就会继续被禁用。

对于暂停,我尝试了我在this fiddle中看到的内容,但它没有用。我也试过这个无济于事:

 XDocument readrooms = XDocument.Load("http://www.website.com/api/api.php?action=getrooms&hotel_id=" + hotel_id3);
            readrooms.Descendants("roomtype").Select(p => new
            {
                roomtypename = p.Attribute("name").Value,
                roomtypeid = p.Attribute("id").Value
            }).ToList().ForEach(p =>
            {
                ComboboxItem itemz = new ComboboxItem();
                itemz.Text = p.roomtypename;
                itemz.Value = p.roomtypeid;
                sel_typeRoom.Items.Add(itemz);

            });

这是fiddle

谢谢!

1 个答案:

答案 0 :(得分:1)

这里有解决工作倒计时的问题:http://jsfiddle.net/XcvaE/4/

<script>

    var CCOUNT = 60;

    var t, count;

    function cddisplay() {
        // displays time in span
        document.getElementById('timespan').innerHTML = count;
    };

    function countdown() {
        // starts countdown
        cddisplay();
        if (count == 0) {
            // time is up
        } else {
            count--;
            t = setTimeout("countdown()", 1000);
        }
    };

    function cdpause() {
        // pauses countdown
        clearTimeout(t);
    };

    function cdreset() {
        // resets countdown
        cdpause();
        count = CCOUNT;
        cddisplay();
    };

</script>

<body onload="cdreset()">
<span id="timespan"></span>
<input type="button" value="Start" onclick="countdown()">
<input type="button" value="Stop" onclick="cdpause()">
<input type="button" value="Reset" onclick="cdreset()">
</body>