【发布时间】:2015-09-02 15:08:58
【问题描述】:
我对此感到非常沮丧...我有一个解决方案,其中 CSS 背景元素源自 Web 服务。目前系统使用HTML刷新整个页面:
<meta http-equiv="refresh" content="300" />
...
body {
background: url('/wallspace/getfile.php?width=gt:1919&height=lt:1700') #1D1F21 top left no-repeat;
background-size: cover;
background-attachment: fixed;
}
这工作得很好,除了它会触发整个页面刷新,这对最终用户来说是不和谐的。我想在 javascript 中执行这个任务,所以它是一个更优雅的过渡,并且不会每 x 秒闪烁整个页面
我不明白为什么这个 SetInterval 函数不起作用
$(window).load(function() {
var path=encodeURI('/wallspace/getfile.php?width=gt:1919&height=lt:1700');
$("body").css("background", "url('default-bg.png')");
setInterval(function(){ callback() }, 5000);
function callback() {
// This works so callback is definitely being triggered
//alert("here");
// This only works the first time --is there caching happening?
$('body').css('background', "url('" + path + "')");
}
});
基本上,回调函数在指定的时间间隔被调用,但它不会改变背景元素。我通过调用警报验证了这一点,它们被触发了。
webservice 的 url 根据它在 GET 请求中收到的查询字符串获取随机图像,但 JQuery 似乎只进行一次调用,然后缓存第一个响应,因为背景图像仅在第一次更改然后在页面完全刷新之前永远不会再出现。
【问题讨论】:
-
尝试在 URL 末尾添加时间戳以避免缓存。
标签: javascript jquery html css setinterval