YUI Library JavaScript DHTML





    
Polling the DataSource

/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
  margin:0;
  padding:0;
}











Polling the DataSource



  

This DataTable polls for data from its DataSource every 5 seconds. Due to browser limitations, implementers should use this feature to replace existing data, rather than continuously add data to the page.


      





YAHOO.util.Event.addListener(window, "load", function() {
    YAHOO.example.Polling = function() {
        var counter = 0;
        var dataIncrementer = function() {
            counter++;
            return [{
                count:counter, 
                description:"At the tone the time will be: ", 
                time: YAHOO.util.Date.format(new Date(), {format:"%I:%M:%S %p"})
            }]
        };
        var myColumnDefs = [
            {key: "count"},
            {key: "description"},
            {key: "time"}
        ];
        var myDataSource = new YAHOO.util.DataSource(dataIncrementer);
        myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
        myDataSource.responseSchema = {
            fields: ["count", "description", "time"]
        };
        var myDataTable = new YAHOO.widget.DataTable("polling",
                myColumnDefs, myDataSource);
                
        // Set up polling
        var myCallback = {
            success: myDataTable.onDataReturnInitializeTable,
            failure: function() {
                YAHOO.log("Polling failure", "error");
            },
            scope: myDataTable
        }
        myDataSource.setInterval(5000, null, myCallback)
                
        return {
            oDS: myDataSource,
            oDT: myDataTable
        };
    }();
});





   
  
yui_2.7.0b.zip( 4,431 k)