Solved

Javascript simple loading image

Posted on 2007-12-05
12
3,729 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 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