在一天中的特定时间插入元素 [英] insert an element at a certain time of the day
问题描述
做某事的最好方法是什么,例如在一天中的某个时间插入元素?我应该使用setTimeout()操作系统吗?有没有更好的方法呢?
what is the best way to do something , like inserting an element at a certain time of the day ? should I use setTimeout() os is there a better way to do it ?
推荐答案
setTimeout()和setInterval()都执行相对于加载时间的定时js语句,而不是每天的特定时间,因此它们不会似乎是一个干净的解决方案.您可以在其中添加一些服务器端脚本.我疲倦的大脑采出了一些随机的解决方案:
setTimeout() and setInterval() both execute timed js statements in relation to when they were loaded, not at a specific time each day, so they don't seem like a clean solution. You could throw a bit of server-side scripting in there. Some random solutions plucked from my tired brain:
- 仅将html元素包装在仅在给定时间执行的条件语句(php,python等)中
- 在页面上具有该元素,但将其设置为display:none.使用php在特定时间添加覆盖display属性的类/样式.
- 同样,使用php添加一个类,但是这次使用js将元素添加到DOM中,并使用新类作为新元素位置的标识符.
如果您想将所有内容都保留在客户端……显然不是很好.
Obviously not great if you want to keep everything client-side... :)
编辑 要在每天下午1点到2点之间(未经测试)在php中执行一条语句:
EDIT To execute a statement in php between 1pm and 2pm every day (untested):
$now = date('G',time());
$start = 13;
$end = 14;
if($now >= $start && $now <= $end){
//do something
}
这篇关于在一天中的特定时间插入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!