Home > Web Front-end > JS Tutorial > body text

Simple code to implement jquery timer_jquery

WBOY
Release: 2016-05-16 17:05:37
Original
974 people have browsed it

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);
});

});
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!