Jscex + jQuery UI

Source: Internet
Author: User

If you have played a table tennis game, you will not be unfamiliar with the concept of strength bar, such:

There are also vertical intensity bars, such:

In fact, similar articles are everywhere! For example, the progress bar when you enter the game, the bar read during the mage's trial in World of Warcraft, and so on ······

By introducing jquery ui, we can easily get the following static strength:

Html:

 
 
  1. <div class="progressbar" style=" width: 20%"> 
  2. /div> 

Js:

 
 
  1. $(function () {  
  2. $(".progressbar").progressbar({  
  3. value: 37  
  4. });  
  5. }); 

The effect is as follows:

Add Jscex to make it work:

 
 
  1. <Script type = "text/javascript">
  2. $ (Function (){
  3. $ (". Progressbar"). progressbar ({
  4. Value: 5
  5. });
  6. });
  7. Var executeAsync = eval (Jscex. compile ("async", function (proceedValues ){
  8. While (proceedValues <100 ){
  9. ProceedValues ++;
  10. $ Await (Jscex. Async. sleep (50 ));
  11. $ (". Progressbar"). progressbar ({
  12. Value: proceedValues
  13. });
  14. }
  15. }));
  16. Function btnExecuteAsync_onclick (){
  17. ExecuteAsync (5). start ();
  18. }
  19. </Script>
  20. <Div class = "progressbar" style = "width: 20%">
  21. </Div>
  22. <Input id = "btnExecuteAsync" type = "button" value = "start" onclick = "return btnExecuteAsync_onclick ()"/>

The effect is as follows:

 

But in general, we need to round-trip infinite loops, so we should achieve this:

 
 
  1. var executeAsync = eval(Jscex.compile("async", function (proceedValues) {  
  2. while (true) {  
  3. while (proceedValues < 100) {  
  4. proceedValues++;  
  5. $await(Jscex.Async.sleep(10));  
  6. $(".progressbar").progressbar({  
  7. value: proceedValues  
  8. });  
  9. }  
  10. if (proceedValues == 100) {  
  11. while (proceedValues > 0) {  
  12. proceedValues--;  
  13. $await(Jscex.Async.sleep(10));  
  14. $(".progressbar").progressbar({  
  15. value: proceedValues  
  16. });  
  17. }  
  18. }  
  19. }  
  20. })); 

The effect is as follows:

 

At this time, I accidentally commented out if (proceedValues = 100) {} and the code looked like this:

 
 
  1. var executeAsync2 = eval(Jscex.compile("async", function (proceedValues) {  
  2. while (true) {  
  3. while (proceedValues < 100) {  
  4. proceedValues++;  
  5. $await(Jscex.Async.sleep(10));  
  6. $(".progressbar3").progressbar({  
  7. value: proceedValues  
  8. });  
  9. }  
  10. //if (proceedValues == 100) {  
  11. while (proceedValues > 0) {  
  12. proceedValues--;  
  13. $await(Jscex.Async.sleep(10));  
  14. $(".progressbar3").progressbar({  
  15. value: proceedValues  
  16. });  
  17. }  
  18. //}  
  19. }  
  20. })); 

I ran it and waited for him to report an ERROR. The result was a miracle. The effect was as follows. It was the same as the above. No ERROR was reported!

It can be seen that the two internal while statements are not executed at the same time, but are very linear. They wait for each other, and the initial execution order is from top to bottom. The internal while statements are executed completely, then jump to the outermost while and re-execute.

This design method is undoubtedly elegant !!

The above three while methods have good semantics. From the analysis, the code can also be written as follows:

 
 
  1. var executeAsync = eval(Jscex.compile("async", function (proceedValues) {  
  2. while (proceedValues < 100) {  
  3. proceedValues++;  
  4. $await(Jscex.Async.sleep(10));  
  5. $(".progressbar").progressbar({  
  6. value: proceedValues  
  7. });  
  8. if (proceedValues == 100) {  
  9. while (proceedValues > 0) {  
  10. proceedValues--;  
  11. $await(Jscex.Async.sleep(10));  
  12. $(".progressbar").progressbar({  
  13. value: proceedValues  
  14. });  
  15. }  
  16. }  
  17. }  
  18. })); 

This is equivalent to Never jumping out of the outermost proceedValues <100, so it will go through an infinite loop.

For the latest Jscex library, please upload ····

More javascript asynchronous programming Series]

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.