不断检查时间是否是下午5点jquery

时间:2016-01-28 03:09:50

标签: javascript jquery time

        function updateTime() {
          var m_names = new Array("Jan", "Feb", "March",
            "April", "May", "June", "July", "Aug", "Sept",
            "Oct", "Nov", "Dec");
          var now = new Date();
          var date = now.getDate();
          var year = now.getFullYear();
          var month = now.getMonth();
          var currentHoursAP = now.getHours();
          var currentHours = now.getHours();
          var currentMinutes = now.getMinutes();
          var currentSeconds = now.getSeconds();
          // Pad the minutes and seconds with leading zeros, if required
          currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
          currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

          // Choose either "AM" or "PM" as appropriate
          var timeOfDay = (currentHours < 12) ? "AM" : "PM";

          // Convert the hours component to 12-hour format if needed
          currentHoursAP = (currentHours > 12) ? currentHours - 12 : currentHours;

          // Convert an hours component of "0" to "12"
          currentHoursAP = (currentHoursAP == 0) ? 12 : currentHoursAP;

          // Compose the string for display
          //var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + " / " + currentHoursAP + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
          var currentTimeString = currentHoursAP + ":" + currentMinutes + " " + timeOfDay;



          //document.getElementById("TimeandDate").value = hour + ":" + minute + " " +"/" + " " + m_names[month] + " " + date + " " + year  ;
          $('#TimeandDate').html("Time:" + currentTimeString + "<br>Date:" + m_names[month] + "/" + date + "/" + year);
          //        $('#Time').text(currentTimeString);
          //        $('#Date').text(m_names[month] + "/" + date + "/" + year);
          return currentTimeString;
        }
        updateTime();
        setInterval(updateTime, 1000); // 5 * 1000 miliseconds





        console.log(updateTime())


        if ((updateTime() == '5:00 PM')) {
          alert(updateTime())
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<span id="TimeandDate"></span>

我想在我的项目中有一个时间检查器。我有一个不断更新时间的功能。现在我想继续检查跨度中的时间是否为5:00 PM因为我需要显示警报。我尝试在函数中返回时间,如

return currentTimeString

然后使用

if ((updateTime() == '5:00 PM')) {
   alert(updateTime())
}
  

出于测试目的,我将条件中的时间更改为最接近的可能时间,但警报未显示。

2 个答案:

答案 0 :(得分:1)

您下午5:00测试的代码只运行一次,您需要在updateTime()内部进行测试才能正确测试

e.g。

function updateTime() {
    // ... code removed for clarity
    var currentTimeString = currentHoursAP + ":" + currentMinutes + " " + timeOfDay;
    // ... code removed for clarity
    if ((currentTimeString == '5:00 PM')) {
        alert("It's 5:00 PM")
    }
}
updateTime();
setInterval(updateTime, 1000); // 5 * 1000 miliseconds

答案 1 :(得分:1)

function isTriggerTime(hour, minute) {
  var now = new Date();
  return (now.getHours() === hour) && (now.getMinutes() === minute);
}

setInterval(function() {
  if(isTriggerTime(17, 0)) {
    alert('it is time');
  }
}, 400);