2013-03-04 50 views
0

我是Appcelerator Titanium APP开发的初学者。从this链接的灵感中,我试图创建一个倒计时器,以便在TableRowView中工作,因为每行都有自己的时间设置。并且我定制这个课程以显示小时分钟和秒钟。在rowview中创建倒计时(钛)

我在每个TableRowView中创建了以下代码以便在列表中执行倒计时。

代码1个

my_timer[timer_index] = new countDown(parseInt(timer_index), parseInt(15), parseInt(50), 
function() { 
    remainingTime.text = ''+my_timer[timer_index].time.h + " : " + my_timer[timer_index].time.m + " : " + my_timer    [timer_index].time.s; 
}, function() { 
    //alert("The time is up!"); 
    } 
); 

my_timer [timer_index ++]启动();

my_time用于推动每一行的倒数计时器的所有实例。

数据来自XHR,因此我创建了一个数组文本来保存代码片段中的所有实例。

问题:当我尝试使用此代码运行我的应用程序,它让我看到一个异常说像“time.h是不确定的”。但是,我在代码中看到了time.h

此外,我可以通过使用单个阵列

例如使用这个类为多个倒计时:

my_timer[0] = new countDown(2,5,5,function(){ 
    somelabel1.text = my_timer[0].time.h+":"+my_timer[0].time.m+":"+my_timer[0].time.s; 
}) 
my_timer[1] = new countDown(2,5,5,function(){ 
    somelabel1.text = my_timer[1].time.h+":"+my_timer[1].time.m+":"+my_timer[1].time.s; 
}) 

上面的代码完美地工作,它有任何错误。但是,如果我尝试在循环中使用此类并传递索引号而不是像代码1中的硬编码值,它将显示如上所述的异常。

任何帮助将非常可观。

预先感谢您。

Desire screen to create with countdown timer in TableRowView

+0

你也许可以说明问题? – 2013-03-04 11:30:22

回答

0

谢谢你的时间和答案。我只是解决了倒计时定制类

var countDown = function(h, m, s, _instance_index, fn_tick, fn_end) { 
     return { 
      total_sec : h * 60 * 60 + m * 60 + s, 
      timer : this.timer, 
      instance_index : _instance_index, 
      set : function(h, m, s) { 
       this.total_sec = parseInt(heart) * 60 * 60 + parseInt(e) * 60 + parseInt(s); 
       this.time = { 
        h : h, 
        m : m, 
        s : s 
       }; 
       return this; 
      }, 
      start : function() { 
       var self = this; 
       this.timer = setInterval(function() { 
        ///alert('running'); 
        if (self.total_sec) { 
         self.total_sec--; 
         var hour = parseInt(self.total_sec/(60 * 60)); 
         var min = (self.total_sec - (parseInt(hour * (60 * 60))) - (self.total_sec % 60))/60; 

         self.time = { 
          h : parseInt(self.total_sec/(60 * 60)), 
          m : parseInt(min), 
          s : (self.total_sec % 60) 
         }; 
         fn_tick(self.time.h + ":" + self.time.m + ":" + self.time.s, self.instance_index); 
        } else { 
         self.stop(); 
         fn_end(); 
        } 
       }, 1000); 
       return this; 
      }, 
      stop : function() { 
       clearInterval(this.timer); 
       this.time = { 
        h : 0, 
        m : 0, 
        s : 0 
       }; 
       this.total_sec = 0; 
       return this; 
      } 
     }; 
    }; 

而且调用这个类使用下面的代码这个问题:

my_timer[items_json.Record.NEW[i].ASSIGN_QUEST_ID] = new countDown(parseInt(n[0]), parseInt(n[1]), parseInt(n[2]), items_json.Record.NEW[i].ASSIGN_QUEST_ID, function(curr_time, instance_index) { 
             questTime[instance_index].text = 'TIME LEFT ' + curr_time; 

            }, function() { 
             //alert("The time is up!"); 
            }); 
            my_timer[items_json.Record.NEW[i].ASSIGN_QUEST_ID].start(); 
0

好吧,如果我猜的话,我也有猜测是因为你没有给我们一个完整的实例或您的问题,甚至说明...

的直觉是您正在循环创建行,并在嵌套函数中引用可变变量(remainingTime)。但是当您转到循环中的下一个项目时,remainingTime会发生变化。因此,当嵌套函数引用它时,它与您最初指定的内容不同,因此只有最后一个计时器正在更新。

这由以下代码演示,它将三次警告“3”。

for (var i = 0; i < 3; i++) { 
    setTimeout(function() { 
     alert(i); 
    }, 100); 
} 

如果你不知道是什么原因,或如何解决它吗,那么我建议你花一些依偎着的火灾与乔一杯茶,一本好书JavaScript的更多的时间。