/    Sign up×
Community /Pin to ProfileBookmark

How to assign different refresh interval to a javascript autorefresh function

Hi there I am calling the following js function after xxx seconds to make a AJAX call to the server. User of this app is permitted to set the variable refresh interval time if he/she wants.

function startTimer(sessionId, jsPageRefreshInterval){
var xmlHttp;
param = “refreshInterval”;

// alert(param + ” = ” + refreshInterval + ” = ” + sessionId + ” = ” + jsPageRefreshInterval);
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}catch (e){
try{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}catch (e){
alert(“Your browser does not support AJAX!”);
return false;
}
}
}

xmlHttp.open(“GET”,”JobControllerUtilServlet?” + “sessionId=” + sessionId + “&paramChanged=true&param=” + param + “&value=” + refreshInterval, true);
xmlHttp.setRequestHeader(“If-Modified-Since”, “Sat, 1 Jan 2000 00:00:00 GMT”);
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 || xmlHttp.readyState==’complete’)
{
if (xmlHttp.status==200){

// alert(xmlHttp.status);
document.getElementById(‘theTable’).innerHTML = xmlHttp.responseText;
dkc=setTimeout(“startTimer(‘” + sessionId + “‘, ‘” + jsPageRefreshInterval + “‘);”, jsPageRefreshInterval);
// return true;
}
}
}
xmlHttp.send(null);
}

The problem is that the existing loop doesn’t stop. (As I have debugged it by alert() messages too). And the new assigned refresh inteval also start refreshing the page. Hence instead of only refreshing the page at the newly assigned interval, it start refreshing the page on diffrenet intervals. Can anyone please help me that what is the reason behind it. I feel that we need to stop the old refresh first and then start the new one. But don’t know how to do that. Would be very greatful if someone help me out.

regards

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienNov 11.2008 — You need to stop the old timeout before starting a new one.
[CODE][B]if (dkc) {
clearTimeout(dkc);
dkc = null;
}[/B]
dkc=setTimeout("startTimer('" + sessionId + "', '" + jsPageRefreshInterval + "');", jsPageRefreshInterval);[/CODE]
Copy linkTweet thisAlerts:
@toicontienNov 11.2008 — I also suggest this code structure, as it could reduce redundant code for later on:
[CODE]/**
* Gets an HTTP request object used for AJAX.
* @param void
* @return object AJAX object, or null if not supported.
*/
function getHttpRequest() {
var i = 0;
var end = getHttpRequest.methods.length;
var req;

while (i < end) {
try {
req = getHttpRequest.methods[i]();
getHttpRequest = getHttpRequest.methods[i];
getHttpRequest.methods = null;
break;
} catch (e) {
continue;
}
i++;
}

return req;
}


/**
* Array of functions used in try-catch loop to create AJAX object
*/
getHttpRequest.methods = [
function() {return new XMLHttpRequest();},
function() {return new ActiveXObject("Msxml3.XMLHTTP");},
function() {return new ActiveXObject("Msxml2.XMLHTTP");},
function() {return new ActiveXObject("Microsoft.XMLHTTP");},
function() {return null}
];


function startAutoRequester(sessionId, jsPageRefreshInterval) {
var xmlHttp = getHttpRequest();
var param = "refreshInterval";

if ( !xmlHttp ) {
return;
}

xmlHttp.open("GET","JobControllerUtilServlet?" + "sessionId=" + sessionId + "&paramChanged=true&param=" + param + "&value=" + refreshInterval, true);
xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 || xmlHttp.readyState=='complete') {
if (xmlHttp.status==200){
// alert(xmlHttp.status);
document.getElementById('theTable').innerHTML = xmlHttp.responseText;
startTimer(sessionId, jsPageRefreshInterval);
// return true;
}
}
};
xmlHttp.send(null);
}


function stopTimer() {
if ( !dkc ) {
return;
}
clearTimeout(dkc);
dkc = null;
}


function startTimer(sessionId, jsPageRefreshInterval) {
if (dkc) {
stopTimer();
}
dkc = setTimeout("startTimer('" + sessionId + "', '" + jsPageRefreshInterval + "');", jsPageRefreshInterval);
}[/CODE]

The code to create an XMLHttpRequest has been pulled out into a separate function. The first time the function runs, it attempts the various methods for creating an AJAX object until one of them succeeds. From the second time onwards, the browser uses the native code it supports for the getHttpRequest function, thereby increasing performance. If a browser does not support AJAX, then a null value is returned by the function, which you can test for else where and avoid the annoying "you're browser doesn't support ajax" alert every time the browser attempts an AJAX connection. In this version of the code, the browser simply fails silently.

You would start the whole process by calling startAutoRequestor() and passing it the session Id and the timeout length in milliseconds.

But this is just another way of doing this, not that you [B]must[/B] do it this way. ?
Copy linkTweet thisAlerts:
@raza_amirauthorNov 12.2008 — Thanks guys...

It worked properly...

Regards
×

Success!

Help @raza_amir spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...