javascript - 不断检查时间是否为5 :00 PM jquery

标签 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 因为我需要显示警报。我尝试返回函数中的时间,就像有一个

返回当前时间字符串

然后使用

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

For testing purposes I change the time in if condition to nearest possible time but the alert is not showing.

最佳答案

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

例如

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

关于javascript - 不断检查时间是否为5 :00 PM jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052178/

相关文章:

javascript - 像框架一样停靠的页脚?

javascript - 当用户单击 anchor 标记时防止默认事件(浏览器导航)

c - 运行 C 程序的运行时间

javascript - 在 react 中映射数组时处理点击

javascript - jQuery .replaceWith 每个只有一次

javascript - 通过此数组 js 将项目添加到循环数组中

javascript - jquery DatePicker 更改下拉列表中的 minDate 选项

javascript - 刷新数据表列

python - 如何获取已知时区字符串的时区偏移量?

javascript - 通过 Javascript 添加迄今为止的小时、分钟和秒,然后显示在标题上