gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:07:00 25 4
gpt4 key购买 nike

        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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com