我是在用于点击支付的时候开启轮询,结束了 就停止
简单例子
<div id="result"></div>
<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.slim.min.js"></script>
<script type="text/javascript">
$(function () {
window.setInterval(function () {
$.get("/timeline",
{"timed": new Date().getTime()},
function (data) {
$("#logs").append("[data: " + data + " ]<br/>");
});
}, 3000);
});
</script>
package com.kaishengit.dao;
import com.kaishengit.entity.Message;
import com.kaishengit.util.DbHelp;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
public class MessageDao {
public List<Message> findAll() {
String sql = "select * from t_message order by id desc";
return DbHelp.query(sql,new BeanListHandler<>(Message.class));
}
public List<Message> findByMaxId(int maxId) {
String sql = "select * from t_message where id > ? order by id desc";
return DbHelp.query(sql,new BeanListHandler<>(Message.class),maxId);
}
}
package com.kaishengit.entity;
public class Message {
private Integer id;
private String message;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.kaishengit.service;
import com.kaishengit.dao.MessageDao;
import com.kaishengit.entity.Message;
import java.util.List;
public class MessageService {
private MessageDao messageDao = new MessageDao();
public List<Message> findAll() {
return messageDao.findAll();
}
public List<Message> findByMaxId(int maxId) {
return messageDao.findByMaxId(maxId);
}
}
package com.kaishengit.web;
import com.google.gson.Gson;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class BaseServlet extends HttpServlet {
/**
* 给客户端响应一个JSON数据
* @param object 要被转成JSON的对象
* @param response
* @throws IOException
*/
public void renderJSON(Object object,HttpServletResponse response) throws IOException {
String json = new Gson().toJson(object);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
out.close();
}
}
package com.kaishengit.web;
import com.google.gson.Gson;
import com.kaishengit.entity.Message;
import com.kaishengit.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/timeline")
public class TimeLineServlet extends BaseServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String maxId = req.getParameter("maxId");
int id = 0;
if(StringUtils.isNumeric(maxId)) {
id = Integer.parseInt(maxId);
}
MessageService messageService = new MessageService();
List<Message> messageList = messageService.findByMaxId(id);
renderJSON(messageList,resp);
}
}
# jdbc config C:\Program Files\Java\jdk1.8.0_101\bin\native2ascii.exe
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///db_22
jdbc.username=root
jdbc.password=rootroot
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="result"></div>
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script>
$(function () {
var maxId = 0;
function call(){
$.get("/timeline",{"maxId":maxId}).done(function(data){
if(data.length) {
for(var i = 0;i < data.length;i++) {
var item = data[i];
var html = "<h3>"+item.message+"</h3>";
if(maxId == 0) {
$(html).appendTo($("#result"));
} else {
$(html).prependTo($("#result"));
}
}
maxId = data[0].id;
}
}).error(function(){
alert("服务器错误");
clearInterval(st);
});
}
call();
var st = setInterval(call,5000);
});
</script>
</body>
</html>