简体   繁体   English

如何在不使用 jquery 但使用 javascript 的情况下使 ajax 每 (n) 秒更新一次?

[英]How make ajax update every (n) number of seconds with out using jquery but using javascript?

I'm trying to hava a javascript poll the server every (n) number of seconds how would I do this with javascript?我正在尝试使用 javascript 每 (n) 秒轮询一次服务器,我将如何使用 javascript 执行此操作?

Assuming you are using jQuery:假设您使用的是 jQuery:

var seconds = 5;

setInterval(function(){
    $.ajax({
        url: 'something.something',
        data: 'something'
    });
}, seconds * 1000)

Without jQuery:不带 jQuery:

var seconds = 5;

setInterval(function(){
    some_ajax_function();
}, seconds * 1000)

Or as @Felix suggests below:或者正如@Felix 在下面建议的那样:

var seconds = 5;
some_ajax_function(seconds);

function some_ajax_function(seconds){
     ..ajax
     onsuccess: setTimeout(function(){some_ajax_function(seconds);}, 
                      seconds * 1000)
}

It is simple with the following function使用以下 function 很简单

window.setInterval("yourfunctionWithAjaxRequestETC", time_in_ms);}); window.setInterval("yourfunctionWithAjaxRequestETC", time_in_ms);});

Enjoy:)享受:)

first, we need to make our ajax request object.首先,我们需要让我们的 ajax 请求 object。 We need to take different browsers into account.我们需要考虑不同的浏览器。

var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else
  {
   // code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

Now, we'll write our function to send a request现在,我们将编写 function 来发送请求

function askData(){
   xmlhttp.open("GET","myinfosource.php",true);  // opens a Get request to the url myinfosource.php, and sets the request to asynchronuous.
   xmlhttp.send(); //sends the request
}

Now, let's write an event handler that changes the HTML when the info comes back.现在,让我们编写一个事件处理程序,当信息返回时更改 HTML。

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) //if we reveived data (readystate 4 means that information was received. status 200 is the status of the HTTP request, where 200 means 'ok'.
    {
    //insert data into the div you want.
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }

} }

And finally, we set an interval on the first function we wrote to make it run every x seconds.最后,我们在我们编写的第一个 function 上设置了一个间隔,使其每 x 秒运行一次。

setInterval('askData',10000);

this will refresh your data.这将刷新您的数据。

I hope you see now why most people use a framework such as jquery to use AJAX.我希望你现在明白为什么大多数人使用 jquery 之类的框架来使用 AJAX。 One of the major advantages of js frameworks is that they work around browser incompatibilities so that you, as the developer can concentrate on the task at hand. js 框架的主要优点之一是它们可以解决浏览器的不兼容性问题,这样开发人员就可以专注于手头的任务。

I assume that there is a servlet with URL Pattern /UpdateCount is configured in web.xml to provide dynamic data/content and there is a div element countStatDiv in the jsp page. I assume that there is a servlet with URL Pattern /UpdateCount is configured in web.xml to provide dynamic data/content and there is a div element countStatDiv in the jsp page.

The following code refreshes/updates the content of countStatDiv at every 30 seconds using GET method and variable seconds value can be changed according to the need:以下代码使用 GET 方法每 30 秒刷新/更新一次countStatDiv的内容,并且可以根据需要更改变量seconds值:

                <script>
                    var request;
                    var seconds=30;
                    function getRequestObject(){
                    setInterval(function() {sendRequest();},seconds*1000);
                    if (window.ActiveXObject){
                    return (new ActiveXObject("Microsoft.XMLHTTP"));
                    } else if (window.XMLHttpRequest){
                    return(new XMLHttpRequest());
                    } else {
                    return (null);
                    }
                    }
                    function sendRequest(){
                    request = getRequestObject();
                    request.onreadystatechange = handleResponse;
                    request.open("GET", "../UpdateCount", true);
                    request.send(null);
                    }
                    function handleResponse(){
                    if((request.readyState == 4)&amp;&amp;(request.status == 200)){
                    var serverResponse = request.responseText;
                    var statCtrl=document.getElementById("countStatDiv");
                    statCtrl.innerHTML=serverResponse;
                    }
                    }
                </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM