NayDream
08-26-2009, 02:40 PM
I'm almost embarrassed to post this because the answer is probably really elementary...but I've been working with it longer than I'd like so I'm looking to you guys who write code much better than I do!
I have a form set up for file uploading, submission works just fine. While the file is loading, a 'loading' animation is displayed and the form itself is invisible. Once the file has loaded successfully, a "success" message is displayed and the form's visibility is switched back on. So far, so good, everything works.
Here's the kicker. I need the page to refresh 10 seconds AFTER the load is completed and the success message is displayed. I can set up the refresh, but no matter how I code this, it doesn't wait for the load to complete before the timer starts. I want visitors to SEE the 'file uploaded successfully' message for 10 seconds before the page refreshes, but the delay isn't working. The page refreshes and the message is never visible.
I have tried using setTimeout and placing it in the 'success' function, but it didn't work, it didn't wait until the message was displayed to start the timer. After some Googling of the problem, I found the idea of getting the time when upload starts and setting it as a variable (t), then getting the time when the load finishes (t2), subtracting the difference, and then using that with the setTimeout function. I wrote it up as a clearOut function and called it in the success function. Still, no go. Obviously I have something basic wrong in my script. Could you guys give me a hand?
var t = 0;
var t2 = 0;
function startUpload(){
document.getElementById('f1_upload_process').style.display = 'block';
document.getElementById('myResponse3').style.visibility = 'hidden';
var d = new Date();
var t = d.getTime();
return true;
}
function clearOut() {
var delay = t2 - t;
var newinterval = delay + 10000;
if (delay < 10000) {
setTimeout(window.location.reload(), newinterval);
}else {
setTimeout(window.location.reload(), 10000);
}
function stopUpload(success){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully.<\/span><br/><br/>';
}
else {
result = '<span class="emsg">There was an error during file upload.<\/span><br/><br/>';
}
document.getElementById('f1_upload_process').style.display = 'none';
document.getElementById('f1_upload_form').innerHTML = result;
document.getElementById('myResponse3').style.visibility = 'visible';
var d2 = new Date();
var t2 = d2.getTime();
clearOut();
return true;
}
}
I have a form set up for file uploading, submission works just fine. While the file is loading, a 'loading' animation is displayed and the form itself is invisible. Once the file has loaded successfully, a "success" message is displayed and the form's visibility is switched back on. So far, so good, everything works.
Here's the kicker. I need the page to refresh 10 seconds AFTER the load is completed and the success message is displayed. I can set up the refresh, but no matter how I code this, it doesn't wait for the load to complete before the timer starts. I want visitors to SEE the 'file uploaded successfully' message for 10 seconds before the page refreshes, but the delay isn't working. The page refreshes and the message is never visible.
I have tried using setTimeout and placing it in the 'success' function, but it didn't work, it didn't wait until the message was displayed to start the timer. After some Googling of the problem, I found the idea of getting the time when upload starts and setting it as a variable (t), then getting the time when the load finishes (t2), subtracting the difference, and then using that with the setTimeout function. I wrote it up as a clearOut function and called it in the success function. Still, no go. Obviously I have something basic wrong in my script. Could you guys give me a hand?
var t = 0;
var t2 = 0;
function startUpload(){
document.getElementById('f1_upload_process').style.display = 'block';
document.getElementById('myResponse3').style.visibility = 'hidden';
var d = new Date();
var t = d.getTime();
return true;
}
function clearOut() {
var delay = t2 - t;
var newinterval = delay + 10000;
if (delay < 10000) {
setTimeout(window.location.reload(), newinterval);
}else {
setTimeout(window.location.reload(), 10000);
}
function stopUpload(success){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully.<\/span><br/><br/>';
}
else {
result = '<span class="emsg">There was an error during file upload.<\/span><br/><br/>';
}
document.getElementById('f1_upload_process').style.display = 'none';
document.getElementById('f1_upload_form').innerHTML = result;
document.getElementById('myResponse3').style.visibility = 'visible';
var d2 = new Date();
var t2 = d2.getTime();
clearOut();
return true;
}
}