Home  >  Article  >  Web Front-end  >  Simple code to implement jquery timer_jquery

Simple code to implement jquery timer_jquery

WBOY
WBOYOriginal
2016-05-16 17:05:37984browse

Simple code to implement jquery timer.

Today, the project encountered a message module, which is at the top of the navigation bar. I want to realize that when a message is received, it can be refreshed in time and display the message bar. After thinking about it for a long time, I thought of 2 methods,

1. Use the Ajax tag of struts2.

2. Use jquery.

struts2 is a bit troublesome, jquery is relatively simple. I used jquery. Paste the code below: Get data from the server every 3 seconds and assign it to the DOM object to be displayed.

Copy code The code is as follows:

$(function(){
run();
var interval;
function run(){
interval = setInterval(chat,"3000");
}
function chat(){
$.post("searchMessage.action",{"mark":"receive","ajax":"ajax" },function(data){
$("span[name='mes']").html(data.num);
},"json");
}
$( "#closeChat").click(function(){
clearTimeout(interval);
});

});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn