我希望按钮在每天晚上10点后停用 [英] I want the button to get disable after 10PM of everyday
问题描述
我想要的是每当页面加载按钮应该被禁用如果当天的当前时间大于晚上10点
我尝试了什么:
我尝试了一些Java脚本但是它没有工作
What I Want Is Whenever The Page Loads The Button Should Get Disabled If The Current Time Of the Day Is Greater Than 10PM
What I have tried:
I Did Try Some Of The Java Scripts But It Isn't Working
<script type="text/javascript" defer="defer">
<!--
var enableDisable = function () {
var UTC_hours = new Date().getUTCHours() + 1;
if (UTC_hours > 22 && UTC_hours < 8) {
document.getElementById('Button1').disabled = true;
}
else {
document.getElementById('Button1').disabled = false;
}
};
setInterval(enableDisable, 1000 * 60);
enableDisable();
// -->
</script>
推荐答案
再次查看if
条件:
Take another look at yourif
condition:
if (UTC_hours > 22 && UTC_hours < 8)
无论 UTC_hours中的值是什么
,它不能同时大于22和小于8。
您可能意味着使用OR而不是AND:
Whatever value is in UTC_hours
, it cannot be both "greater than 22" and "less than 8" at the same time.
You probably mean to use an "OR" instead of an "AND":
if (UTC_hours > 22 || UTC_hours < 8)
然而,这只会是d 晚上11点按钮。在晚上10:59,小时仍然是10,所以它不是大于10 。
如果你想在晚上10点,您需要更改第一个条件:
However, this will only disable the button at 11 PM. At 10:59 PM, the hour is still 10, so it's not greater than 10.
If you want to disable the button at 10 PM, you need to change the first condition:
if (UTC_hours >= 22 || UTC_hours < 8)
您还应该记住,这是根据用户的时钟使用UTC时间。它不会使用您服务器的时区;它不会使用您客户的时区;如果客户端的时钟错误,代码将在错误的时间启用/禁用按钮。
You should also bear in mind that this is using UTC time based on the user's clock. It won't use your server's time-zone; it won't use your client's time-zone; and if the client's clock is wrong, the code will enable/disable the button at the wrong time.
触发处理程序的时间应该是可变的,具体取决于执行时间。代码设置时间,但你使用常量。我认为没有理由检查其余部分;首先解决这个问题。你已经有了实时;剩下的就这样了。
我也会用setTimeout
而不是setInterval
:
WindowTimers.setTimeout() - Web API | MDN [ ^ ]。
-SA
The time to trigger the handler should be variable, depending on the time of execution of the code setting the time, but you use the constant. I see no reason to check up the rest; first fix this problem. You already got real time; so do the rest.
I would also usesetTimeout
instead ofsetInterval
:
WindowTimers.setTimeout() — Web APIs | MDN[^].
—SA
这篇关于我希望按钮在每天晚上10点后停用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!