Titanium自动更新剩余时间标签

时间:2014-04-29 12:12:22

标签: javascript android datetime titanium titanium-mobile

我正在尝试在Titanium开发的Android应用中创建一个标签,该应用会根据我的输入(从当前日期到2014年5月30日)自动更新剩余时间。我用JavaScript创建了以下倒计时功能:

var current = 'Today is the day!';
var montharray = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
function countdown(yr,m,d){
    theyear = yr;
    themonth = m;
    theday = d;
    var today = new Date();
    var todayy = today.getYear();
    var todaym = today.getMonth();
    var todayd = today.getDate();
    var todayh = today.getHours();
    var todaymin = today.getMinutes();
    var todaysec = today.getSeconds();
    var todaystring = montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
    futurestring = montharray[m-1]+" "+d+", "+yr;
    dd = Date.parse(futurestring) - Date.parse(todaystring);
    dday = Math.floor(dd/(60*60*1000*24)*1);
    dhour = Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
    dmin = Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
    dsec = Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
    if (dday==0 && dhour==0 && dmin==0 && dsec==1){
        homeLabel6.text = current;
        return;
    } else {
        homeLabel6.text = dday + 'd:'+ dhour + 'h:' + dmin + 'm:' + dsec + 's';
        setTimeout('countdown(theyear,themonth,theday)',1000);
    }
}
countdown(2014,05,30);

不幸的是,当我在手机上构建应用时,我收到以下错误:

[ERROR] :  TiApplication: (KrollRuntimeThread) [821,821] Sending event: exception on thread: KrollRuntimeThread msg:java.lang.IncompatibleClassChangeError: interface not implemented; Titanium 3.2.2,2014/03/05 12:22,96e9a07
[ERROR] :  TiApplication: java.lang.IncompatibleClassChangeError: interface not implemented
[ERROR] :  TiApplication:   at ti.modules.titanium.TitaniumModule$Timer.run(TitaniumModule.java:152)
[ERROR] :  TiApplication:   at android.os.Handler.handleCallback(Handler.java:615)
[ERROR] :  TiApplication:   at android.os.Handler.dispatchMessage(Handler.java:92)
[ERROR] :  TiApplication:   at android.os.Looper.loop(Looper.java:137)
[ERROR] :  TiApplication:   at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112)

如果有人能够向我解释,为了使这项工作不相容,可能会很棒。我只想要一个标签显示这个没有开始/停止按钮或类似的东西。就像上面在代码片段中提到的那样,我想要信息的标签叫做homeLabel6。我已经尝试了其他选择,但它们似乎都不适合我的需要。

1 个答案:

答案 0 :(得分:3)

setTimeout方法接受函数和持续时间,并且您正在传递字符串来代替函数setTimeout('countdown(theyear,themonth,theday)',1000);

试试这个:

setTimeout(function(){ countdown(theyear,themonth,theday); },1000);

或者如果您想每1秒更新一次文本,那么您可以使用setIntervalsetInterval(function(){ countdown(theyear,themonth,theday); },1000);