2016-04-28 149 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