Timer refresh page with ajax + js + jquery

107 5

I want to display a message module on a jsp page, which requires a timely update, and how do you use ajax + js + jquery to implement. The js framework used is jquery.

5 Answers

83 2

Hello, you say that polling is going to pull out information on the server, and the first way is that it's time to request a notification at the front end of every 30 seconds, and then return to the front html tag implementation without refresh update notification, I think this is the logic. Here's the code

/*第一次读取最新通知*/
 setTimeout(function() {
 Push();
 },
 200);
/*30轮询读取函数*/
 setInterval(function() {
 Push();
 },
 30000);
/*请求函数的ajax*/
function Push() {
 $.ajax({
 type:"POST",
 url:"/index.php?s=Push&a=index",
 data: {
 t: 3
 },
 beforeSend: function() {},
 success: function(data) {
 var obj = eval("(" + data +")");
//alert(obj.sixin);
 if (obj.sixin!= 0) {
 $(".tongzhi").html(obj.sixin).show();
 } else {
 $(".tongzhi").html(0).hide();
 }
 }
 });
60 3
...