Js round-robin server and js round-robin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.