我不确定这是否可能,但我可以在服务器秒内刷新我的div,而不是jquery计时器?
或者可能在加载所有图像之前不启动计时器?
这种方法很有效,但它经常不同步,可能是因为一些图像仍在尝试加载。
此代码来源于在线
标记:
refreshing in <div id="countDown"></div>
10秒后刷新div:
$(document).ready( function(){
$(' #avatars').load(' #avatars');
refresh();
});
function refresh()
{
setTimeout( function() {
$(' #avatars').load(' #avatars');
refresh();
}, 10000);
}
Jquery 15秒计时器,在0 后重置回15
window.onload = function() {
startCountDown(10, 1000, myFunction);
}
function startCountDown(i, p, f) {
var pause = p;
var fn = f;
var countDownObj = document.getElementById("countDown");
countDownObj.count = function(i) {
// write out count
countDownObj.innerHTML = i;
if (i == 0) {
// execute function
fn();
startCountDown(10, 1000, myFunction);
// stop
return;
}
setTimeout(function() {
// repeat
countDownObj.count(i - 1);
}, pause);
}
// set it going
countDownObj.count(i);
}
function myFunction(){};
如果你只想延迟计时器直到你的图像加载,你不能在$('#avatars'(上使用回调函数。load('#avatars';启动计时器?
function refresh(){
$('#avatars').load('#avatars', function(){
setTimeout(function(){
refresh();
}, 10000);
startCountDown(10, 1000, myFunction);
});
}
我相信,在图像加载完成之前,这不会开始倒计时。
加载状态为成功时调用刷新函数。请参阅此处了解更多信息http://api.jquery.com/load/
$("#avatars").load(url, function(responseTxt, statusTxt, xhr) {
// debugger;
if (statusTxt == "success")
{
refresh();
}
if (statusTxt == "error")
{
console.log("Error: " + xhr.status + ": " + xhr.statusText);
}
});