2017-08-10 88 views
0

我一直在與我一直在努力的代碼艱難的時間。否則如果陳述不起作用

對於上下文,我正在編程一個簡單的Tumblr頁面(不是我的博客主題,只是一個頁面),其中音樂,背景和gif將根據一天的時間而改變。在經歷了許多挫折和反覆試驗之後,我得到了大部分工作 - 除了一件事。

但是,我只能每天三次(日,日落,夜間),因爲每當我試圖包含「其他如果」它不起作用時,我只能白天和黑夜。這裏是我當前的代碼,確實工作,但只有一天兩次了三個(夜/日落)的:

var currentTime = new Date().getHours(); 
 
if (19 <= currentTime && currentTime < 6) { 
 
    if (document.body) { 
 
     document.body.background = "(night bg)"; 
 
     var url = "(night gif)"; 
 
     var img = new Image(); 
 
     img.src = url; 
 
     document.body.appendChild(img); 
 
     var source = "(night music)"; 
 
     var audio = new Audio(); 
 
     audio.addEventListener("load", function() { 
 
     audio.play(); 
 
     }, true); 
 
     audio.src = source; 
 
     audio.autoplay = true; 
 
     audio.loop = true; 
 
    } 
 
} 
 

 
else { 
 
    if (document.body) { 
 
     document.body.background = "(sunset bg)"; 
 
     var url = "(sunset gif)"; 
 
     var img = new Image(); 
 
     img.src = url; 
 
     document.body.appendChild(img); 
 
     var source = "(day music)"; 
 
     var audio = new Audio(); 
 
     audio.addEventListener("load", function() { 
 
     audio.play(); 
 
     }, true); 
 
     audio.src = source; 
 
     audio.autoplay = true; 
 
     audio.loop = true; 
 

 

 
    } 
 
}

我試圖改變

else { 
    if (document.body) { 
     document.body.background = "(day bg)"; 

變成了「其他if」而不是僅僅「else」在頂部,並且還增加了一個時間,如下所示:

else if (16 <= currentTime && currentTime < 19) { 
    if (document.body) { 
     document.body.background = "(day bg)"; 

時沒有工作,我嘗試過許多東西,就像脫離了「如果」或使得無論是「如果」和「別人」到「否則,如果」但我已經得到了什麼工作。我還試圖製作第三個塊,其餘的編碼只是「其他」,但這也不起作用。有人知道解決方案和/或我做錯了什麼嗎?

+4

if(19 <= currentTime && currentTime <6)是否正確?我認爲..如果(19 <= currentTime II currentTime <6) – zynkn

回答

1

我認爲你必須改變你的代碼。

if(19 < = currentTime & & currentTime < 6)不正確。

因爲這意味着19 < = X < 6.這是不可能的。

所以,你應該改變,如果(19 < = currentTime的|| currentTime的< 6)

var currentTime = new Date().getHours(); 
 
if (19 <= currentTime || currentTime < 6) { 
 
    if (document.body) { 
 
     document.body.background = "(night bg)"; 
 
     var url = "(night gif)"; 
 
     var img = new Image(); 
 
     img.src = url; 
 
     document.body.appendChild(img); 
 
     var source = "(night music)"; 
 
     var audio = new Audio(); 
 
     audio.addEventListener("load", function() { 
 
     audio.play(); 
 
     }, true); 
 
     audio.src = source; 
 
     audio.autoplay = true; 
 
     audio.loop = true; 
 
    } 
 
} 
 
else if(16 <= currentTime && currentTime < 19){ 
 
    /* programming Here */ 
 
} 
 
else { 
 
    if (document.body) { 
 
     document.body.background = "(sunset bg)"; 
 
     var url = "(sunset gif)"; 
 
     var img = new Image(); 
 
     img.src = url; 
 
     document.body.appendChild(img); 
 
     var source = "(day music)"; 
 
     var audio = new Audio(); 
 
     audio.addEventListener("load", function() { 
 
     audio.play(); 
 
     }, true); 
 
     audio.src = source; 
 
     audio.autoplay = true; 
 
     audio.loop = true; 
 

 

 
    } 
 
}

+1

代碼轉儲不是很好的答案。給它添加一些解釋 –

+1

沒有解釋的答案是不完整的。請解釋什麼和你爲什麼改變。請記住,你正在爲讀者回答。 – Rajesh

+0

對不起傢伙。其實我不熟悉英文和計算器。並感謝您的建議。我會盡力解釋我的代碼。 – zynkn

-1
var currentTime = new Date().getHours(); 
if (currentTime > 19 && currentTime < 6) { 
    // Night 
} else if (currentTime > 6 && currentTime < 17) { 
    // Day 
} else { 
    // Sunset 
} 

詳見here

+0

請解釋什麼和你爲什麼改變。您正在爲讀者作答,而不僅僅是OP – Rajesh

1

我認爲您的解決方案可以改進。如果你看看你的代碼,你會發現每個事件都有很多重複。

如果你想添加另一個事件,還有更多的代碼要添加。

我建議你在陣列中進行,而不是時間表。這使得添加新事件變得更容易。

// Define the schedule. start= start hour, end = end hour. 
// With start=6 and end=19, the schedule will be used between 
// 06:00 - 18:59. 
// The schedules will be searched in order, and the first matching 
// schedule will be used. Therefore the default schedule (night) must 
// be last in the list. 
var schedule = [ 
    { start : 6, end:19, bbg:"(sunset bg)", img:"(sunset gif)", music:"(sunset music)" }, 

    // Default schedule, must be last in list. 
    { start : 0, end:24, bbg:"(night bg)", img:"(night gif)", music:"(night music)" } 
]; 

var currentHour = new Date().getHours(); 
var currentSchedue; 

// Loop though schedules  
for (var idx=0; idx < schedule.length; idx+=1) { 
    currentSchedule = schedule[idx]; 

    // Find first matching schedule 
    if (currentSchedule.start <= currentHour && currentSchedule.end > currentHour) { 
    document.body.background = currentSchedule.bbg; 

    var img = new Image(); 
    img.src = currentSchedule.img; 
    document.body.appendChild(img); 

    var audio = new Auido(); 
    audio.src = currentSchedule.music; 
    audio.autoplay = true; 
    audio.loop = true; 

    break; // stop loop if matching schedule was found 
    } 
} 

如果你仍然想使用if語句,這裏是一個工作示例:

if (6 <= currentHour && currentHour < 16) { 
    // Daytime, valid from 06:00 - 15:59 
    console.log('Day', currentHour); 
} 
else if (16 <= currentHour && currentHour < 19) { 
    // Evening valid from 16:00 - 18:59 
    console.log('Evening', currentHour); 
} 
else { 
    // Night, valid 00:00 - 05:59 and 19:00 - 24:00 
    console.log('Night', currentHour); 
} 

邏輯錯誤在你的if語句

if (19 <= currentTime && currentTime < 6) { 
} 
  • 19 <= currentTime僅當currentTime爲19或更高時才成立
  • currentTime < 6只有當currentTime小於6時才爲true。

它們不能同時爲真。