2016-04-28 152 views
1

Noob警报.....计时动画gif显示

我已经搜索了以前的问题,但无法找到此特定请求。

对于我的艺术项目,我创建了一个动画gif,并希望它在我为其他项目创建的网站上每天显示/运行我的动画gif ,每天只有一小时

我有这个脚本非常相似,但我需要自动化显示(每天一个小时),而不是点击或分步。我可以摆脱这些阶段,但不知道如何替换它们。

JavaScript动画

<script type="text/javascript"> 
    <!-- 
     var imgObj = null; 
     var animate ; 

     function init(){ 
      imgObj = document.getElementById('myImage'); 
      imgObj.style.position= 'relative'; 
      imgObj.style.left = '0px'; 
     } 

     function moveRight(){ 
      imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px'; 
      animate = setTimeout(moveRight,20); // call moveRight in 20msec 
     } 

     function stop(){ 
      clearTimeout(animate); 
      imgObj.style.left = '0px'; 
     } 

     window.onload =init; 
    //--> 
    </script> 

预先感谢您.....

+0

我不知道你在问什么这里..问题是时间检测? – Jordumus

+0

是的,我想是的。我需要gif在每24小时显示一小时。 – giveimtamongo

回答

0

好了,要注意的第一件事是,你会必须时不时查看当前时间,以便chec k如果我们在某个时刻想要显示动画。我们举个例子:13点到14点(下午1点到2点) - 从现在开始,我会用24h表示法。

我们可以使用interval来检查它是否已经过了13h。我们自己选择精度。为了举例,假设我们每5分钟检查一次:

//Note: we could say "300000" immediately instead, but with the calculation, we can easily change it when we want to. 
var gifInterval = setInterval(function() {canWeAnimateYet();}, 5 * 60 * 1000); 

因此,我们得到了一个间隔,每5分钟检查一次。现在,我们需要一个flag(或bool),看是否动画应该运行或不...

var animateThatGif = false; 

现在。我们需要的功能来检查时间:

var canWeAnimateYet = function() { 
    //Checks here. 
} 

在那功能,我们需要检查当前时间。如果已经过了13日但是在14h之前,我们需要将我们的国旗加到true,否则它会保留false

var canWeAnimateYet = function() { 

    //Get current time 
    //Note: "new Date()" will always return current time and date. 
    var time = new Date(); 

    //Note: getHours() returns the hour of the day (between 0 and 23 included). Always in 24h-notation. 
    if (time.getHours() >= 13 && time.getHours < 14) 
     animateThatGif = true; 
    else 
     animateThatGif = false; 
} 
+0

谢谢你.....帮助和工作的一种享受。感谢您花时间和麻烦..问候 – giveimtamongo

+0

@giveimtamongo如果它帮助你,请考虑投票并标记为答案:) – Jordumus