回答
如果您知道在什么时间淡入(ahem),那么只需计算出那段时间(据推测在未来)与我们的时间之间的增量。例如:
var dateNow = new Date();
setTimeout(function() {
doFadingInStuff();
}, dateWanted - dateNow);
如果你当前的时间,并计算所需的事件发生(因此你必须经过的时间,直到你的事件应该发生的量)的未来时,你可以使用setTimeout()
功能在未来的准确时间安排一个函数调用。从这个功能,你会做你的动画。
假设你只需要一定的时间过程中的东西是可见的(即淡入时,它变成小时,淡出时,它已经不再是小时),你可以这样做:
// element is assumed to be a jQuery object
var VISIBLE_HOUR=14; // 2:00 PM
function check() {
var isHour=(new Date()).getUTCHours()==VISIBLE_HOUR;
var isVisible=element.is(":visible");
if(isHour!=isVisible) {
element.fadeToggle(1000);
}
}
// We probably don't want to check very frequently...
// You could make it more advanced by checking
// more frequently closer to the hour in which it would be visible.
setInterval(check, 30000);
var eventHour = 16, // 4:00pm
// get the current time as a Date
now = new Date(),
// turn your event time into a Date
eventDate = new Date(now.getFullYear(),
now.getMonth(),
now.getDate(),
eventHour),
// calculate how many MS until your event
eventTimeMS = eventDate - now;
dayInMS = 86400000,
// your event
myEvent = function() {
alert('The time is now!');
};
// adding 24 hours if already past event time today
eventTimeMS = eventTimeMS < 0 ? eventTimeMS + dayInMS : eventTimeMS;
// if currently the right hour, just invoke event
if (eventHour == now.getHours()) {
myEvent();
// otherwise start a timer to invoke your event at the appropriate time
} else {
setTimeout(myEvent, eventTimeMS);
}
有什么方法可以将时间更改为小时和分钟吗?我认为这符合我的需求,但为了测试它,我希望能够立即看到效果。感谢您的回答:) – monogatari
是的,如果您查看Date对象如何在Javascript中工作,您应该能够自己弄清楚。 – mVChr
我想你想检查客户端的时间,然后淡入或淡出。这就要做到像这样:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
var sunriseHour = 1;
var sunsetHour = 19;
function checkForChange() {
var nowDate = new Date();
var nowHour = nowDate.getHours();
if ((nowHour >= sunriseHour) && (nowHour < sunsetHour)) {
if (sunPosition != 'up') {
sunPosition = 'up';
$('#theSun').fadeIn('slow');
}
} else {
if (sunPosition != 'down') {
sunPosition = 'down';
$('#theSun').fadeOut('slow');
}
}
}
var nowDate = new Date();
var nowHour = nowDate.getHours();
if ((nowHour >= sunriseHour) && (nowHour < sunsetHour)) {
$('#theSun').show();
sunPosition = 'up';
} else {
$('#theSun').hide();
sunPosition = 'down';
}
setTimeout(checkForChange, 1000);
});
</script>
</head>
<body>
<div id="theSun" style="background: yellow; width: 300px; height: 300px; display: none;">
This box is the sun
</div>
</body>
</html>
这是小提琴:http://jsfiddle.net/dCLFA/ –
- 1. android:在某些时间做某事
- 2. 在一天中的某个时间淡入淡出元素
- 3. JavaScript淡入淡出时间
- 4. 计算时间在某些状态的时间序列数据
- 5. 仅在某些时间显示iframe
- 6. 在某些时间安排批次
- 7. 自定义某些时刻人性化的持续时间值
- 8. 某些API在iOS上格式化时间增量
- 9. 在随机时间间隔内淡入/淡出
- 10. 获取某些UNIX时间的日期
- 11. 当某些日期时间经过
- 12. 如何某些特定时间
- 13. 链接在某些时间之间可见
- 14. Ruby On Rails - Resque Scheduler - 在某些时间段之间更频繁
- 15. android:在某些特定时间间隔内报警remian停止
- 16. 在某些时间间隔的Linux函数调用
- 17. 可以在VS2015之间的某些时间阻止签入
- 18. 在某些时间段之间更改背景图片
- 19. Oracle数据库:在一天的某些时间之间选择
- 20. jQuery在类之间淡化
- 21. NSDate的时间或!时间在它
- 22. MySQL在某些时区显示错误的时间
- 23. 在某段时间后重新加载某些div
- 24. 在Python中打开某个网页某些时间
- 25. 让Rails在某个特定时间发生某些事情
- 26. asp.net - 在某段时间内禁用某些内容
- 27. SQL日期时间转换为特定日期,如果在某些时间
- 28. Python和OpenCV - 获取视频在某些时间点的持续时间
- 29. 如何在android中关闭应用程序时使用某些时间间隔?
- 30. jQuery淡入淡出divs超过设置的时间间隔
注意,如果超时是长(说24小时后的)这个代码可能会破坏(在某些浏览器火立即)。请参阅http://stackoverflow.com/questions/3468607/why-does-settimeout-break-for-large-millisecond-delay-values –
确保函数在执行后自毁,或者设置一个标志来指示它已被执行以防止它再次执行。 – bhagyas