当php返回更改时,使用javascript更新html代码
问题描述:
我想使用返回的php文件连续更新html文档。因此,我需要使用jQuery函数:
I want to continuously update an html document using the return of a php file. Therefore I need to use the jQuery function:
$.get('returnfunction.php', function(data) {
test=data;
document.getElementById('t1').innerHTML=test;
});
如何在javascript中连续调用此函数?
How can I call this function continuously in javascript? setInterval seems not proper for this asynchronous function.
感谢
答
使用 setInterval
调用异步函数的问题是,如果服务器或网络较慢,未应答的请求可能会累积。
The problem with calling an asynchronous function using setInterval
is that the unanswered requests might accumulate if the server or the network is slow.
您可以这么做:
(function doOne(){
$.get('returnfunction.php', function(data) {
test=data;
document.getElementById('t1').innerHTML=test;
setTimeout(doOne, 1000);
});
})();
这样下一次调用 $。get
您可能还希望根据您的具体要求对失败采取行动,而不仅仅是成功。
You might also want to act on failures, not just on success, depending on your exact requirements.