Solved

Hide progress loader after success

Posted on 2009-03-30
2
571 Views
Last Modified: 2012-05-06
I am working on a AJAX/JSON driven commenter and I am using jQuery's AJAX function before send i have a progress ticker pop up then when it runs i want to have it hide:

beforeSend: function(){$("#add_post").fadeOut("fast",function loadAnimation(){$("#loading").show();$("p.error").hide();});
                        },
                        success: function(latest){
                              $("#loading").hide();
                              newContent = '<div id="post-'+ latest.id +'">\n';
                              newContent += '<h3>' + latest.author + '</h3>\n';
                              newContent += '<p class="small quiet">' + latest.date_added + '</p>\n';
                              newContent += '<p>' + latest.post_content + '</p>';
                              newContent += '<hr/>';
                              $("#contents").append(newContent).fadeIn("slow");
                        }

So I have the animated GIF in #loading, so beforeSend, show the element, then on success hide it. But it seems to just keep it showing all the time unless i refresh the page. Any idea what I can do to have it hide after it sends and show the form (i know i would just fade the form back in)

Thoughts?

Ryan
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Facebook like ajax post - jQuery - ryancoughlin.com</title>

<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen, projection" />

<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />

<!--[if IE]><link rel="stylesheet" href="css/ie.css" type="text/css" media="screen, projection"><![endif]-->

<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">

/* <![CDATA[ */

$(document).ready(function(){

	var content    = "";

	var newContent = "";

	$('p.validate').hide();

	

	$.getJSON("readJSON.php",function(data){

		$.each(data.posts, function(i,post){

			content = '<div id="post-'+ post.id +'">\n';

			content += '<h3>' + post.author + '</h3>\n';

			content += '<p class="small quiet">' + post.date_added + '</p>\n';

			content += '<p>' + post.post_content + '</p>';

			content += '<hr/>';

			$("#contents").append(content).fadeIn("slow");		

		});

	});  

	

	$(".reload").click(function() { 

		$("#posts").empty();

	});

	

	$("#add_post").submit(function() {

		$('p.validate').empty();

		

		// we want to store the values from the form input box, then send via ajax below

		var author = $('#author').attr('value');

		var post   = $('#post').attr('value'); 

		

		if(($("#author").val() == "") && ($("#post").val() == "")){

			$("p.validate").fadeIn().append("Both fields are required.");

			$('#author,#post').fadeIn().addClass("error");

		}else{

			$.ajax({

				type: "POST",

				url: "ajax.php",

				data: "author="+ author + "&post=" + post,

				dataType: "json",

				beforeSend: function(){$("#add_post").fadeOut("fast",function loadAnimation(){$("#loading").show();$("p.error").hide();});

				},

				success: function(latest){

					$("#loading").hide();

					newContent = '<div id="post-'+ latest.id +'">\n';

					newContent += '<h3>' + latest.author + '</h3>\n';

					newContent += '<p class="small quiet">' + latest.date_added + '</p>\n';

					newContent += '<p>' + latest.post_content + '</p>';

					newContent += '<hr/>';

					$("#contents").append(newContent).fadeIn("slow");

				}

			});

		

	}

	return false;

	});

	

});

/* ]]> */

</script>

<style type="text/css">

h3{margin:10px 0;}

p{margin:5px 0;}

#posts{display:none;}

#loading{text-align:center;}

</style>

</head>

<body>

        <div class="container">

                <div class="span-24">

						<div id="post-container" class="span-9 colborder">

						    	<h2>Posts Loaded Via AJAX/JSON:</h2>

								<div id="contents"></div>	

						</div>

						<div id="form" class="span-11">

							<h2>New Post:</h2>

						

							<form name="add_post" id="add_post" action="">

								<label>Author:</label><br />

								<input type="text" name="author" id="author" size="15" class="text" /><br />

								<label>Post:</label><br />

								<textarea name="post" id="post" rows="5" cols="5" class="text"></textarea><br />

								<input type="submit" value="Post" id="submit" /><br />

							</form><br />

							<div id="loading" style="display:none">

							    <img src="images/loading.gif" alt="Loading" />

							</div>

							<p class="validate error"></p>

							

						</div>	

                </div>

				<div class="span-24">

					<a href="#" class="reload">Reload</a>

				</div>

        </div>

</body>

</html>

Open in new window

0
Comment
2 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 total points
Comment Utility
Is the call successfull?
There could be an error as well which is not handled.
You can also specify the complete function wich allways gets executed no matter if the call is successfull or not.
                        $.ajax({

                                type: "POST",

                                url: "ajax.php",

                                data: "author="+ author + "&post=" + post,

                                dataType: "json",

                                beforeSend: function(){$("#add_post").fadeOut("fast",function loadAnimation(){$("#loading").show();$("p.error").hide();});

                                },

                                success: function(latest){

                                        newContent = '<div id="post-'+ latest.id +'">\n';

                                        newContent += '<h3>' + latest.author + '</h3>\n';

                                        newContent += '<p class="small quiet">' + latest.date_added + '</p>\n';

                                        newContent += '<p>' + latest.post_content + '</p>';

                                        newContent += '<hr/>';

                                        $("#contents").append(newContent).fadeIn("slow");

                                },

                                complete: function() {

                                        $("#loading").hide();

                                }  

                        });

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
Let me give this a go! Thanks for your help.

Ryan
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now