2017-01-02 11 views
0

現在、格納されているajaxレスポンスの値によっては、ボディの背景を修正しようとしています。しかし、関数getBackground()を呼び出すとき。それは背景を更新していないと私はなぜ理解していないのですか?更新する関数をバックグラウンドで更新しない理由

$(document).ready(function(){ 
    // Ajax call 

    var responseWeather = $.parseJSON($.ajax({ 
    url: urlLatLong, 
    dataType: "json", 
    async: false 
}).responseText); 


    function getBackground(){ 
    var n = new Date(); 
    var hour = n.getHours(); 


    if (hour > 6 && hour < 18) { 
     //response from Ajax 
    if (responseWeather.weather[0].main === 'Rain') {  
    $('body').css('background-image', "https://4.bp.blogspot.com/-FRc-pTtmDaE/VESPnFw7e2I/AAAAAAABSIw/g_YMwPEPvRQ/s0/Rain%2BDrops%2BWindow%2BUHD.jpg"); 
}else if (responseWeather.weather[0].main === 'Clouds') { 
    $('body').css('background-image', "http://wallpapersonthe.net/wallpapers/b/3840x2400/3840x2400-sky_nature_cloudy_day_in_the_alpine_jungle_cloud_mountain_jungle-30477.jpg"); 
}else if (responseWeather.weather[0].main === 'Clear') { 
    $('body').css('background-image', "https://wallpaperscraft.com/image/sun_light_beams_meadow_glade_summer_day_53997_3840x2400.jpg"); 
} 
    }else{ 

    if (responseWeather.weather[0].main === 'Rain') { 
    $('body').css('background-image', 'https://images3.alphacoders.com/279/279307.jpg'); 
}else if (responseWeather.weather[0].main === 'Clouds') { 
    $('body').css('background-image', 'http://www.walldevil.com/wallpapers/a68/night-background-cloudy-wallpaper-animated-skies-sky-wallpapers-art.jpg'); 
}else if (responseWeather.weather[0].main === 'Clear') { 
    $('body').css('background-image', 'https://s-media-cache-ak0.pinimg.com/originals/c6/28/0e/c6280e8f909cdaa96f599f4b611e876c.jpg'); 
    } 
    } 
} 
getBackground(); 


}); 
+1

こんにちは。コンソールにエラーがありますか?そしてresponseWeatherはどこから来たのですか? –

+0

が更新されました。これはAjaxコールからの応答です。 – ontiverosvzla

+0

ありがとうございます - あなたが受け取ったJSONのサンプルを投稿することができます。たとえば、 'responseWeather.weather [0] .main ===" sunshine "'とすると、あなたの見ているものが説明されます。 catch-all else節がないifがあります。 –

答えて

1

にイメージURLをラップするのを忘れないでください。

$('body').css('background-image', "url(https://...)"); 
+0

あまりにもそれを逃した、ありがとう。 – ontiverosvzla

関連する問題