更改使用jQuery和AJAX背景图片 [英] Changing background image using jQuery and AJAX
问题描述
我试图写一个code异步加载的背景下每一个N秒。
I'm trying to write a code to load asynchronously a background every "N" seconds.
OK!现在一切正常!但15秒后的第一个图像开始。如何加载在页面加载的结束?
OK! NOW EVERYTHING WORKS! But the first image start after 15 seconds. How to load at the end of page load?
(function(jQuery) {
window.setInterval(function() {
jQuery.ajax({
url: 'http://example.com/background.php',
}).done(function(data) {
console.log("done Ok!");
var newImage = new Image();
newImage.onload = function() {
jQuery('body').css({"background-image": data, "color": "red !important"});
console.log("ok2!");
};
newImage.src = data;
});
}, 15000);
})(window.jQuery);
问题解决了,但现在我需要如何在后台加载的背景图片。
QUESTION SOLVED, BUT NOW I NEED how to load in background the background image.
下面的问题: <一href="http://stackoverflow.com/questions/25766943/changing-background-image-using-jquery-and-ajax-but-async-and-in-background">Changing使用jQuery和Ajax,但异步和背景背景图片
Here the question: Changing background image using jQuery and Ajax, but Async and in background
推荐答案
将循环功能出了 window.interval
通话,因为它本身的功能。
Move the looping function out of the window.interval
call, as it's own function.
(function(jQuery) {
function intervalFunc() {
jQuery.ajax({
url: 'http://example.com/background.php',
}).done(function(data) {
console.log("done Ok!");
var newImage = new Image();
newImage.onload = function() {
jQuery('body').css({"background-image": data, "color": "red !important"});
console.log("ok2!");
};
newImage.src = data;
});
}
window.setInterval(intervalFunc, 15000); // Start the interval timer
intervalFunc(); // Run once first after page load
})(window.jQuery);
这篇关于更改使用jQuery和AJAX背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!