• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 604
  • Last Modified:

Hide progress loader after success

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
catonthecouchproductions
Asked:
catonthecouchproductions
1 Solution
 
Albert Van HalenAnalyst developerCommented:
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
 
catonthecouchproductionsAuthor Commented:
Let me give this a go! Thanks for your help.

Ryan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now