http://www.code4coder.com is a source where coders can find different solutions in .Net, Oracle and related technologies.
Search This Blog
Polling in JavaScript (Object Oriented Way)
Polling in JavaScript (Object oriented way).
Problem:
In certain cases, when polling is required; E.g. in my case there is one grid on web-page (asp.net) which needs to be refreshed through AJAX after some interval. Normally we will write a couple of functions and some variables to accomplish this task. What if we need to use same functionality for 2 different grids on same page? (or on different pages) (Most of the code will be replicated). To avoid replication we will poll our data through code written in object-oriented way.
Solution:
Following is class in javaScript:
//Note : Don't get confuse with word 'function' written before 'Poll' J….. This is how a class is defined in javaScript.
function Poll(funcName){
/*Data Members*/
this._timerID = 0;
this.functionName = funcName;
/*Member Methods*/
this.StartPolling = function(){
if (!this.isPolling()){
this._timerID = window.setInterval(this.functionName, _pollingInterval);
}
}
this.StopPolling = function(){
window.clearInterval(this._timerID);
this._timerID = 0;
}
this.isPolling = function(){
return (this._timerID != 0);
}
this.getPollingFunction = function(){
return
this.functionName;
}
}
// Now declare two different objects for each grid: (Note, refreshGrid_1() & refreshGrid_2() are functions written in JavaScript, which are calling web-service to refresh data in particular grid:
var pollGrid_1 = new Poll("refreshGrid_1();");
var pollGrid_2 = new Poll("refreshGrid_2();");
//Use these objects to poll function
pollGrid_1.StartPolling();
pollGrid_2.StartPolling();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment