2011-09-09 40 views
2

我在想这是否可能?对于一个网站概念,我想根据用户的计算机时间在某个小时内弹出或淡入。我尝试过无法找到,因为我不知道什么样的函数可以控制什么时候发生效果。在某些时间或时间淡化

任何想法?谢谢。

回答

5

如果您知道在什么时间淡入(ahem),那么只需计算出那段时间(据推测在未来)与我们的时间之间的增量。例如:

var dateNow = new Date(); 

setTimeout(function() { 
    doFadingInStuff(); 
}, dateWanted - dateNow); 
+0

注意,如果超时是长(说24小时后的)这个代码可能会破坏(在某些浏览器火立即)。请参阅http://stackoverflow.com/questions/3468607/why-does-settimeout-break-for-large-millisecond-delay-values –

+0

确保函数在执行后自毁,或者设置一个标志来指示它已被执行以防止它再次执行。 – bhagyas

2

如果你当前的时间,并计算所需的事件发生(因此你必须经过的时间,直到你的事件应该发生的量)的未来时,你可以使用setTimeout()功能在未来的准确时间安排一个函数调用。从这个功能,你会做你的动画。

0

假设你只需要一定的时间过程中的东西是可见的(即淡入时,它变成小时,淡出时,它已经不再是小时),你可以这样做:

// 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); 
0

检查这项fiddle

你的JS

var now = new Date().toString(); 

    $('#fadeMe').html(now).fadeIn(9000) 
0
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); 
} 
+0

有什么方法可以将时间更改为小时和分钟吗?我认为这符合我的需求,但为了测试它,我希望能够立即看到效果。感谢您的回答:) – monogatari

+0

是的,如果您查看Date对象如何在Javascript中工作,您应该能够自己弄清楚。 – mVChr

0

我想你想检查客户端的时间,然后淡入或淡出。这就要做到像这样:

<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> 
+0

这是小提琴:http://jsfiddle.net/dCLFA/ –