Solved

Javascript simple loading image

Posted on 2007-12-05
12
3,726 Views
Last Modified: 2013-11-19
Hi

I am trying to show a loading animated gif when a form is submitted, and pause the submission for about 4-5 seconds.  I have some code, but when it is submitted, the form 'freezes' for 4 seconds, then it will quickly show loading image and submit it.





function loading(millisecondi)
{
		document.getElementById('loading').innerHTML = '<img src="/loading.gif">';
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;
 
    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

Open in new window

0
Comment
Question by:logicjb
12 Comments
 
LVL 18

Accepted Solution

by:
Morcalavin earned 168 total points
ID: 20411614
Couldn't you just use window.setTimout to send the transmission at a particular time instead of blocking your entire script execution(and your browser)?
<script type="text/javascript">
function delay(myform)
{
document.getElementById('loading').innerHTML = '<img src="/loading.gif">';
window.setTimeout(function(){myform.submit()}, 4000);
}
</script>
 
<form action="whatever.html">
<input type="text" value="I am a text box"/><input type="button" value="Submit" onclick="delay(this.form)"/>
</form>

Open in new window

0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20411626
Stuffed the button code.  It should be:

<input type="button" value="Submit" onclick="delay(this.form)">

Open in new window

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 166 total points
ID: 20411641
Yes. You cannot loop inside a function to emulate a "wait"

Instead use setTimeout

I assume you do not have an onSubmit on the form tag if you do you need to tell me

1. change the submit button to

<script>
document.write('<input type="button" value="submit" onClick="loading(this.form.name,3000)">')
</script>
<noscript>
<input type="submit">
</script>

2 change your function to

function loading(formname,milliseconds) {
  document.getElementById('loading').innerHTML = '<img src="/loading.gif">';
  setTimeout('document.forms["'+formName+'"].submit()',milliseconds);
}



Alternatively use a hidden iframe, show a div on submit and have the form result hide it again
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20411684
Morcalavin you also stuffed the passing of the form

The function that does the submit does not know what form was passed when it eventually executes

As you can see from my code you need something like

var theForm = ""
function delay(myform) {
  theForm = myform;
  document.getElementById('loading').innerHTML = '<img src="/loading.gif">';
  window.setTimeout(function(){theForm.submit()}, 4000);
}


I have never used a function inside a setTimeout - I normally pass a functioname in quotes...
Interestingly yours works too
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20411723
@mplungjan

No I didn't.
<input type="button" value="Submit" onclick="delay(this.form)">

This button passes the form object to be submitted.

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20412093
Yes you did

function delay(myform)
{
document.getElementById('loading').innerHTML = '<img src="/loading.gif">';
window.setTimeout(function(){myform.submit()}, 4000); <<<<<<<< when THIS anonymous function executes, there is no more "myform" object available to it
}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20412167
No you didn't... How on earth does THAT work???

<script>
function delay(myform) {
document.getElementById('loading').innerHTML = 'loading';
window.setTimeout(function(){ if(myform) { alert('submitting '+ myform.name);  myform.submit()} else alert('What form???')}, 4000);
}
</script>

<form name="form1" action="http://www.google.com/search">
<input type="text" name="q" value="javascript scope">
<input type="button" onClick="delay(this.form)" value="Delayed submit">
</form>
<span id="loading"></span>
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20412286
Using an anonymous function inside another function extends the scope of the variable to the anonymous function itself.  For instance, this works too.  Click submit, then the text box, then submit2.  You get 'bar' every time, even though foo appears to go out of scope when it's wrapped in the anonymous function.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
<script type="text/javascript">
function delay(myform)
{
      var foo = 'bar';
      document.getElementById('id').onclick = function(){alert(foo)};
}

</script>
 
<form action="whatever.html">
<input id="id" type="text" value="I am a text box"/><input type="button" value="Submit" onclick="delay(this.form)"/>
<input type="button" value="Submit2" onclick="document.getElementById('id').onclick()"/>
</form>
</body>
</html>
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20412300
I don't know why the scope inside an anonymous function carries over the way it does.
0
 
LVL 16

Assisted Solution

by:LeeKowalkowski
LeeKowalkowski earned 166 total points
ID: 20568558
It's called a 'closure', http://www.jibbering.com/faq/faq_notes/closures.html.  They're not easy to spot all the time, and can lead to serious memory leaks particularly in Internet Explorer, especially when accidentally created over large collections.

--
Lee
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20953179
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question