对我的 jQuery 代码进行小修改
您好,如何编辑以下代码以自动重新加载指定 div 的内容?
我下面的代码重新加载一个名为 form.php 的文件,我想用 div 替换它。
<style>
.loading {
height:24px;
background: url('http://b.static.ak.fbcdn.net/rsrc.php/v1/yb/r/GsNJNwuI-UM.gif') 50% 50% no-repeat;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js'></script>
<script language='JavaScript'>
setInterval( 'SANAjax();', 10000 ); ///////// 10 seconds
$(function()
{
SANAjax = function()
{
$('#reservationdetails')
.empty()
.addClass('loading')
.load('form.php', function()
{
$('#reservationdetails').removeClass('loading')
});
}
});
</script>
谢谢你!
Hello how can I edit my following code to reload automatically the content of a specified div?
My code below reloads a file called form.php and I would like to be replaced with a div.
<style>
.loading {
height:24px;
background: url('http://b.static.ak.fbcdn.net/rsrc.php/v1/yb/r/GsNJNwuI-UM.gif') 50% 50% no-repeat;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js'></script>
<script language='JavaScript'>
setInterval( 'SANAjax();', 10000 ); ///////// 10 seconds
$(function()
{
SANAjax = function()
{
$('#reservationdetails')
.empty()
.addClass('loading')
.load('form.php', function()
{
$('#reservationdetails').removeClass('loading')
});
}
});
</script>
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重新排序您的代码:
我假设
#reservationdetails
是您想要加载响应的 div。无需将字符串传递给
setInterval
。始终将函数引用传递给它(与setTimeout
相同)。Reorder your code:
I assumed that
#reservationdetails
is the div you want to load the responce in.There is no need to pass a string to
setInterval
. Always pass function reference to it (same forsetTimeout
).菲利克斯·克林的回答是正确的。
另外值得注意的是:以这种方式使用
setInterval
是非常糟糕的做法,因为您不确定.load
是否会在您指定的 10 秒内返回。 (例如,考虑移动设备。)即使它确实在 9 秒内返回,在您发出下一个请求之前也只需要一秒钟。最好在回调中执行setTimeout
,如下所示:Felix Kling's answer is correct.
Also worth noting: it is very bad practice to use
setInterval
in this manner, as you are unsure if.load
will return within the 10 seconds you specified. (Think, for example, of mobile devices.) Even if it did return in 9 seconds, it would then be only one second before you fired off the next request. Better to do asetTimeout
in the callback, like so: