Search code examples
extjsextjs3

Update time in extjs automatically


I want to show current time on a window in my app which is written using Extjs 3. The time should be updated every one second but I don't know how to do this. this is my code: Can anyone help me please?

function gettime(){
        var dt = new Date();
        dt = dt.format('h:i:s');
        return dt;
    };

var clock = { layout:'form', frame:false, region:'center', height:100, width:400,
items:[{id: 'currtime', xtype: 'displayfield',fieldLabel: 'Current Time'
,value:gettime()}]}`

Solution

  • Use the TaskManager or the vanilla setInterval function to run your updating code periodically.

    Edit

    Example:

    // Keep a ref, in case you want to stop it later
    var task = Ext.TaskMgr.start({
        interval: 1000
        ,run: function() {
            Ext.getCmp('currtime').setValue(gettime());
        }
    });