JavaScript Countdown Timer每天到特定时间

时间:2016-05-21 02:32:49

标签: javascript jquery

我目前正在开发一个名为倒数计时器的网站:http://iphone.myhandykey.com/

当前计时器只有12小时+几分钟..我想要的是倒数计时器将显示当前访客时区的剩余时间直到晚上11点。那可能吗?谢谢!

Here is the JavaScript:



function startTimer(duration, display) {
    var timer = duration, hours, minutes, seconds;
        setInterval(function () {



        hours = parseInt(((timer / 60) / 60 ) % 60, 10);



        minutes = parseInt((timer / 60)%60, 10);
        seconds = parseInt(timer % 60, 10);

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

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



        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

window.onload = function () {
    var onehour = 60 * 600 * 1.231,
        display = document.querySelector('#time');
    startTimer(onehour, display);
};

这是HTML:

<span id=time></span>

编辑:如果访客的当前时间是例如晚上11点40分,它应该显示23小时&amp;还剩20分钟..

2 个答案:

答案 0 :(得分:2)

(function() {
  var start = new Date;
  start.setHours(23, 0, 0); // 11pm

  function pad(num) {
    return ("0" + parseInt(num)).substr(-2);
  }

  function tick() {
    var now = new Date;
    if (now > start) { // too late, go to tomorrow
      start.setDate(start.getDate() + 1);
    }
    var remain = ((start - now) / 1000);
    var hh = pad((remain / 60 / 60) % 60);
    var mm = pad((remain / 60) % 60);
    var ss = pad(remain % 60);
    document.getElementById('time').innerHTML =
      hh + ":" + mm + ":" + ss;
    setTimeout(tick, 1000);
  }

  document.addEventListener('DOMContentLoaded', tick);
})();
Only <span id='time'></span> left!

答案 1 :(得分:0)

这样的事情:

    Try
        sqlConn = New MySqlConnection
        connStr = New String("Server = localhost; Database = gen_database; Uid = root; Pwd =")
        sqlConn.ConnectionString = connStr
        myCommand = New MySqlCommand("Select DevCompanyName from developer_name_table; Select DevType from development_type_table")
        myCommand.CommandType = CommandType.Text
        myCommand.Connection = sqlConn
        ComboBox1.Items.Clear()
        sqlConn.Open()
        MsgBox("Connection Open.")
        dR = myCommand.ExecuteReader()
        Do While dR.Read()
            ComboBox1.Items.Add(dR("DevCompanyName"))
            ComboBox2.Items.Add(dR("DevType")) 'Error shows here Could not find specified column in results: DevType
        Loop
    Catch ex As MySqlException
        MsgBox(ex.ToString)
    Finally
        dR.Close()
        sqlConn.Close()
    End Try