2012-10-08 72 views
1

我在Sencha-2的按钮上显示当前时间。时间正在更新,但只有一次。 我要不断更新用自动时间更新

下面是我的代码: -

Ext.define("Stackoverflow.view.Demo", { 
    extend: "Ext.Container", 
    alias: "widget.demo", 
    config: { 

      items: [{ 
       xtype: "toolbar", 
       id: 'clocktool', 
       docked: "bottom", 
       items: [ 
        { 
         xtype: 'button', 
         itemId: "clock", 
         id:'clock', 
         text: Ext.Date.format(new Date(),'g:i:s A') 

        } 
       ] 
      }] 

     }, 

     initialize: function(){ 
     console.log("initializing main view"); 
     Ext.defer(this.refreshDate, 1000, this); 

     }, 
     refreshDate: function() { 
     console.log("refreshing date"); 
     var btn = Ext.getCmp('clock'); 
     btn.setText(Ext.Date.format(new Date(),'g:i:s A')); 
     console.log("done"); 
     } 

    }); 

在此先感谢。在sencha-2展示时间的任何其他方法也受到欢迎。

回答

2

当包含键启动的观点,只是做这样的事情:

Ext.defer(this.refreshDate, 1000, this); 

然后中庸之道创建一个名为refreshDate功能:

refreshDate: function() { 
    var btn = ... // get your button 

    btn.setText(Ext.Date.format(new Date(),'g:i:s A')); 

    Ext.defer(this.refreshDate, 1000, this); 
} 

希望这有助于

+0

我已经更新我的代码(通过添加你的代码)。它正在工作,但只有一次。你能帮助我,我缺乏什么。 –

+0

我忘了一些东西。您需要在refreshDate中调用refreshDate,以便创建一个无限循环。 –

+0

感谢您的帮助。它的工作现在。 –