Simple code to implement jquery timer.
Today, the project encountered a message module at the top of the navigation bar. to achieve this, You can promptly refresh the received message and display the number of received messages. I thought for a long time. I thought of two methods,
1. Use the Ajax tag of struts2.
2. Use jquery.
Struts2 is a little troublesome. jquery is relatively simple to use jquery. The following code is attached: get data from the server every 3 seconds. Then assign the value to the dom object to be displayed.
Copy codeThe 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 ']" pai.html (data. num );
}, "Json ");
}
$ ("# CloseChat"). click (function (){
ClearTimeout (interval );
});
});