我不明白你为什么要使用计时器,你可以简单地在 ajax 请求开始时将其打开,然后在成功或错误时将其关闭,例如:
$('#loading').show();
$.post(url, request)
.success(function(data) {
$('#loading').hide();
// Do what you want with data
})
.error(function(data) {
$('#loading').hide();
// Show error message
});
要将加载图像放在页面中间,请确保它的容器在 100% 高度的父级内,如果它是嵌套的,请确保其父级都不是position:relative
.
#loading {
position:relative;
width:100%;
height:100%;
}
#loading img {
margin-top: -12px; // Half images height;
margin-left: -12px; // Half images width;
position:absolute;
top:50%;
left:50%;
}
OR
#loading {
width:100%;
height:100%;
}
#loading img {
width:24px // Images width
height:auto; // If Image size changes don't mess with proportions
margin:0 auto;
margin-top:48%;
}