当php返回更改时,使用javascript更新html代码 [英] Updating html code with javascript when php return changed

查看:122
本文介绍了当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.

这篇关于当php返回更改时,使用javascript更新html代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆