Js round-robin server and js round-robin
<Script type = "text/javascript"> function showUnreadNews () {$ (document ). ready (function () {$. ajax ({type: "GET", url: "bridgeAction. action ", dataType:" json ", success: function (msg) {if (msg. data) {if (msg. data. szdata) {if (confirm (msg. data. szdata + 'are incoming calls answered? ') {$. Ajax ({type: "GET", url: "bridgejieAction. action ", dataType:" json ", success: function (data) {if (data. status = "success") {setTimeout ('showunreadnews () ', 1000) ;}}) ;}else {$. ajax ({type: "GET", url: "bridgejuAction. action ", dataType:" json ", success: function (data) {if (data. status = "success") {setTimeout ('showunreadnews () ', 1000) ;}}}}else {setTimeout ('showunreadnews ()', 1000) ;}} else {setTimeout ('showunreadnews () ', 1000 );}}});});} </script>
Solve js polling Problems
/*
Cyclic functions:
Parameters:
CircleArray: cyclic Array
Padding: discard the unit of time (for example, 3 indicates discard every 3)
Index: starts the index loop (0 by default)
*/
Function circle (circleArray, padding, index ){
Var arrayReturn = [];
Var indexPlus = index;
For (var I = 0; I <circleArray. length; I ++ ){
If (I> 0) & (I + indexPlus) % padding> 0 )){
ArrayReturn. push (circleArray [I]);
}
Index = (I + indexPlus) % padding;
}
If (arrayReturn. length> 1 ){
Return circle (arrayReturn, padding, index)
}
Else if (arrayReturn. length = 1 ){
Return arrayReturn [0];
}
Else {
Return circleArray [0];
}
}
/*
Obtain array functions:
Parameters:
Max: the maximum value in the array (from 1 to max)
*/
Function getArray (max ){
Var array = [];
For (var I = 1; I <= max; I ++ ){
Array. push (I );
}
Return array;
}
Var last = circle (getArray (100), 3, 0); // last is the last number.
I wrote a round robin using js. How can I write it to stop it under the condition to help me?
Use clearInterval, as shown below:
$ (Document). ready (function (){
C = setInterval (checkIsExist, 10000); // execute the checkIsExist method every 10 seconds.
});
Function checkIsExist (){
$. Ajax ({
Type: "POST ",
Url: "/SecondServlet ",
Data: "date =" + new Date (),
Success: function (rtmsg ){
If (rtmsg = 'exist '){
$ ("# Download" cmd.html ("<button onclick = \" window. open ('/ArticleServlet'); \ "> download </button> ");
Window. clearInterval (c );
}
}
});
}