执行一个Ajax请求每一秒 [英] Execute an Ajax request every second

查看:164
本文介绍了执行一个Ajax请求每一秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX调用正在作出一个PHP文件。我收到的结果。现在我调查是否有可能有Ajax请求自动执行每1秒。我张贴的成果转化为输入字段名为隐藏。我该如何执行AJAX调用每隔三秒钟,而无需调用该函数?

  $。阿贾克斯({
            键入:POST,
            网址:increment.php,
            数据:$(本).serialize()
            数据类型:JSON,
            成功:功能(数据){
                    $('#隐藏)VAL(数据); //先设定值

            }
    });
 

解决方案

你可能要考虑的东西是的服务器发送的事件(上证所)

这是一个HTML5技术,即用JavaScript长轮询服务器端点(您PHP文件),看是否有变化时有发生。长轮询基本上是JS的地方(我不知道,如果它使用Ajax或其他技术)发送一个请求每秒到端点

您可以尝试这样的:

 #/ your_js
VAR evtSource =新的EventSource(increment.php);
evtSource.onmessage =功能(E){
    $('#隐藏)VAL(e.data)。
}
 

要发送的数据,可以使一个AJAX调用将发送更新的JSON对象到服务器,就像你有:

  $(文件)。在(点击,.your_object,功能(数据){
     $阿贾克斯({
                键入:POST,
                网址:increment.php,
                数据:$(本).serialize()
                数据类型:JSON
        });
   });
 

当你执行一个事件这样只会打开一个Ajax请求,您的应用程序将被听为响应每一秒。如你所知,阿贾克斯长轮询是超资源密集型的,所以它会更好看网络插座的东西,如果你想真正的实时的技术,但无论哪种方式,这将是一个更有效的系统比只用普通的ajax的一切

下面一个警告 - 你必须改变你的 increment.php 来处理不同的响应类型

I have an ajax call being made to a php file. I am receiving results. Now I am investigating if it is possible to have the ajax request automatically perform every 1 second. I am posting the results into input field called hidden. How can I execute the ajax call every three seconds without having to call the function?

    $.ajax({
            type: 'POST',
            url: 'increment.php',
            data: $(this).serialize(),
            dataType: 'json',
            success: function (data) {
                    $('#hidden').val(data);// first set the value     

            }
    });

解决方案

Something you might want to consider is Server Sent Events (SSE's)

This is an HTML5 technology whereby Javascript will "long-poll" a server endpoint (your PHP file) to see if any changes have occurred. Long-polling is basically where JS (I'm not sure if it uses Ajax or another technology) sends a request every second to the endpoint

You can try it like this:

#/your_js
var evtSource = new EventSource("increment.php");
evtSource.onmessage = function(e) {
    $('#hidden').val(e.data);
}

To send the data, you can make an ajax call which will send the updated JSON object to the server, like you have:

  $(document).on("click", ".your_object", function(data) {
     $.ajax({
                type: 'POST',
                url: 'increment.php',
                data: $(this).serialize(),
                dataType: 'json'
        });
   });

This will only open an Ajax request when you perform an event, and your app will be "listening" for the response every second. As you are aware, Ajax long-polling is super resource-intensive, so it will be better to look at web-socket stuff if you want true "real-time" technology, but either way, this will be a much more efficient system than just using plain ajax for everything

A caveat here -- you'll have to change your increment.php to handle the different response types

这篇关于执行一个Ajax请求每一秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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