论坛首页 AJAX版

初学Js,捣鼓了一个Sheduler(更正)

浏览 1820 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2006-11-30
搜索一下,没有找到。对那些ajax库也不熟悉,不知道哪里找。

只好把java下常用的一个简单的模型搬了过来。

从java转型过来,对js最大的感受就是:不能按照习惯来确定你写的东西是什么意思,颇让人忐忑不安。

补充:指点。已经改好。

js 代码
 
  1. //------------------------------------------------------  
  2. // 一个简单的例子。  
  3. //-------------------------------------------------------  
  4. var scheduler1 = new Scheduler();  
  5.   
  6. var task1 = new SchedulerTask();  
  7.   
  8. task1.run = function(){  
  9.     alert("mytask 1 running");  
  10. }  
  11.                                                                
  12. var iter1 = new SchedulerIterator();  
  13.   
  14. iter1.nextInterval = function(){  
  15.     return 4000;  
  16. }  
  17.   
  18. scheduler1.schedule(task1,iter1);  
   
时间:2006-11-30
能详细一点介绍吗?来点截图看看效果?
   
0 请登录后投票
时间:2006-11-30
发现不能在ie下运行,不知道什么问题。。。。大侠指点一下
   
0 请登录后投票
时间:2006-11-30
楼主,网页上有错误!
   
0 请登录后投票
时间:2006-11-30
写法都很普通啊,在ff下没有问题,但是ie下有错误。。。郁闷中

很简单的东东,才几行代码,有一个测试页面。没有必要截图吧。
   
0 请登录后投票
时间:2006-11-30

zhangqidi 写道:
写法都很普通啊,在ff下没有问题,但是ie下有错误。。。郁闷中

很简单的东东,才几行代码,有一个测试页面。没有必要截图吧。

faint!在flock上也不行啊!



   
0 请登录后投票
时间:2006-11-30
麻烦大侠给看看哈,不知道什么地方有问题。我这儿firefox运行是正常的。

js 代码
 
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  4. <script type="text/javascript" language="JavaScript" src="scheduler.js"></script>  
  5. </head>  
  6. <body>  
  7.   
  8. <script type="text/javascript">  
  9.   
  10.   
  11. // 有一个对象:Scheduler,两个标签接口:SchedulerTask和SchedulerIterator  
  12.   
  13. var Scheduler = function(){};  
  14.   
  15. Scheduler.run = function(task,iterator){  
  16.     task.run.call(task);  
  17.     nextInterval = iterator.nextInterval.call(iterator);  
  18.     if ( nextInterval <= 0){  
  19.         // stop run. do nothing.  
  20.     } else {  
  21.         window.setTimeout(Scheduler.run,nextInterval,task,iterator);  
  22.     }  
  23. }  
  24.   
  25. Scheduler.prototype.schedule = function(task,iterator){  
  26.     nextInterval = iterator.nextInterval.call(iterator);  
  27.     if ( nextInterval <= 0 ){  
  28.         // stop run. do nothing.  
  29.     } else {  
  30.         window.setTimeout(Scheduler.run,nextInterval,task,iterator);  
  31.     }  
  32. }  
  33.   
  34. // markup interface. must have a run() function  
  35. var SchedulerTask= function(){  
  36.   
  37. }  
  38.   
  39. // markup interface. must have a nextInterval() function.  
  40. var SchedulerIterator =  function(){  
  41.   
  42. }  
  43.   
  44. // 下面是三个使用例子  
  45.   
  46. //------------------------------------------------------  
  47. // task1 is simple repeating intervally.  
  48. //-------------------------------------------------------  
  49. var s1 = new Scheduler();  
  50.   
  51. var mytask1 = new SchedulerTask();  
  52.   
  53. mytask1.run = function(){  
  54.     alert("mytask 1 running");  
  55. }  
  56.                                                                
  57. var myiter1 = new SchedulerIterator();  
  58.   
  59. myiter1.nextInterval = function(){  
  60.     return 3000;  
  61. }  
  62.   
  63. //------------------------------------------------------  
  64. // task2运行三次就中止  
  65. //------------------------------------------------------  
  66. var s2 = new Scheduler();  
  67.   
  68. var mytask2 = new SchedulerTask();  
  69.   
  70. mytask2.run = function(){  
  71.     alert("mytask2 running. count:" + myiter2.count);  
  72. }  
  73.   
  74. var myiter2 = new SchedulerIterator();  
  75. myiter2.count = 0;  
  76. myiter2.nextInterval = function(){  
  77.     if ( ++ this.count > 3 ){  
  78.         return -1;  
  79.     } else {  
  80.         return 2000;  
  81.     }  
  82. }  
  83.   
  84. //------------------------------------------------------  
  85. // task3根据运行情况决定执行时间。  
  86. //------------------------------------------------------  
  87. var s3 = new Scheduler();  
  88.   
  89. var mytask3 = new SchedulerTask();  
  90. mytask3.shouldStop = false;  
  91. mytask3.data;  
  92. mytask3.run = function(){  
  93.     this.data = Math.random();  
  94.     if ( this.data > 0.7){  
  95.         this.shouldStop = true;  
  96.         alert("task3 **stopped****. data:" + this.data);  
  97.     } else {  
  98.         alert("task3 running. data:" + this.data);  
  99.     }  
  100.   
  101. }  
  102.   
  103. var myiter3 = new SchedulerIterator();  
  104. myiter3.nextInterval = function(){  
  105.     if ( !mytask3.shouldStop){  
  106.         return 3000;  
  107.     } else {  
  108.         return -1;  
  109.     }  
  110. }  
  111.   
  112. var testStart = function(){  
  113.     s1.schedule(mytask1,myiter1);  
  114.     s2.schedule(mytask2,myiter2);  
  115.     s3.schedule(mytask3,myiter3);  
  116.   
  117. }  
  118.   
  119. window.onload = function(){  
  120.   
  121.     testStart();  
  122.   
  123. }  
  124.   
  125. </script>  
  126.   
  127.   
  128.   
  129.   
  130.   
  131.   
  132. </body>  
  133. </html>  

   
0 请登录后投票
时间:2006-11-30
Scheduler.prototype.schedule = function(task,iterator){   
    nextInterval = iterator.nextInterval.call(iterator);   
    if ( nextInterval <= 0 ){   
        // stop run. do nothing.   
    } else {   
        //这句为啥这么写?
        window.setTimeout(Scheduler.run,nextInterval,task,iterator);
    }   
}   
   
0 请登录后投票
时间:2006-11-30
unifly 写道
Scheduler.prototype.schedule = function(task,iterator){   
    nextInterval = iterator.nextInterval.call(iterator);   
    if ( nextInterval <= 0 ){   
        // stop run. do nothing.   
    } else {   
        //这句为啥这么写?
        window.setTimeout(Scheduler.run,nextInterval,task,iterator);
    }   
}   




MSDN上说明:
window.setTimeout(vCode, iMilliSeconds [, sLanguage])

本来的确最后一个参数是传给前面的函数当参数的,但是邪恶的IE没有实现,FF等实现了。

所以要重载window.setTimeout()来兼容。
   
0 请登录后投票
时间:2006-11-30
谢谢
   
0 请登录后投票
论坛首页 AJAX版

跳转论坛: