/    Sign up×
Community /Pin to ProfileBookmark

AJAX loading progress question

I can use the following script to load a text file
using the asynchronous AJAX function.

I found it from a reference on this forum and it works well.

I wanted to delay until the entire text file is loaded
so that I can process the loaded information further.

I thought I should be able to use the ‘readyState’
to populate a progress bar. I understand that the text
file should be pretty large or the display doesn’t change much.
The progress bar was just something to do while the text file loaded.

The problem I’m having is that if I don’t put the ‘alert’ message
in the delay loop logic,
the script never changes the ‘CurrentReadyState’ flag to ‘4’ (I think)
so that the processing can proceed.
With the ‘alert’ all works as expected and I can process the loaded information.

Without the ‘alert’, the script times out.

? Any one have a recommendation or am I just wrong with my intended implementation of this AJAX attempt?

Here’s the script (just substitute a text file to load)

[code=php]
<html>
<head>
<META Http-Equiv=”Cache-Control” Content=”no-cache”>
<META Http-Equiv=”Pragma” Content=”no-cache”>
<META Http-Equiv=”Expires” Content=”0″>
<title>AJAX status during load</title>

<script type=”text/javascript”>
// From: http://www.w3schools.com/ajax/ajax_serverscript.asp
var ReadyStateFlag = 0;
var CurrentReadyState = 0;

function ajaxFunction(filename,storeTo) {
var xmlHttp;
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.onreadystatechange=function() {
// var CurrentReadyState = xmlHttp.readyState;
CurrentReadyState = xmlHttp.readyState;
if (CurrentReadyState==4) {
document.getElementById(storeTo).value=xmlHttp.responseText;
ReadyStateFlag = 4;
}
}
xmlHttp.open(“GET”,filename,true);
xmlHttp.send(null);
}

function Retrieve() {
while (ReadyStateFlag != 4) {
document.getElementById(‘progress’).innerHTML = ‘Progress: ‘+’XXXX’.substring(0,CurrentReadyState);

// remove following ‘alert’ and script end up timing out and not loading text file
// alert(‘Loading: ‘+CurrentReadyState);
}
document.getElementById(‘progress’).innerHTML = ‘Progress: ‘+’XXXX’.substring(0,CurrentReadyState);
}

function CollectInfo(fromArea,toArea) {
ajaxFunction(fromArea,toArea);
Retrieve();
ReadyStateFlag = 0;
}
</script>
</head>
<body>
<center>
<h1> AJAX test load </h1>

<a href=”javascript:void(0)”
onClick=”CollectInfo(‘BQ200701.txt’,’TxtInfo’)”>Load text file</a>
<!– Change ‘BQ200701.txt’ to your own text file name for testing purposes –>

<br /><span id=”progress”></span>
<br />
<textarea id=”TxtInfo” rows=”10″ cols=”80″></textarea>
</center>
</body>
</html>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERauthorDec 06.2007 — I tried removing all the 'ReadyStateFlag' logic in the first post

and replaced the Retrieve() AND CollectInfo() functions like this:
[code=php]
function CollectInfo(fromArea,toArea) {
document.getElementById(toArea).value = '';
ajaxFunction(fromArea,toArea);
var cnt = 0;
while (document.getElementById(toArea).value == '') {
cnt++; alert('Wait: '+cnt);
}
}
[/code]

Still works fine with alert() message in the while loop,

but if I take out alert() only, the program stops in a timeout error.

What am I doing wrong or what is it that I don't understand about this AJAX stuff while doing asynchronous data file loads?
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...