Optimize jQuery code

Posted by Dannemannen on Stack Overflow See other posts from Stack Overflow or by Dannemannen
Published on 2011-01-17T22:51:43Z Indexed on 2011/01/17 22:53 UTC
Read the original article Hit count: 152

Greetings,

Just built some stuff with jQuery, everything works perfect(!), but I would like it to be as optimzed as possible.. what small changes can I do to my code?

  $(document).ready(function() {

 // hide the indicator, we use it later
 $(".indicator").hide();

 // start the animation of the progressbar 
 $(".fill").animate({ width: "50px",}, 4000, function() { $(".indicator").effect("pulsate", { times:999 }, 2000);});

 // notify-me ajax function
 $(".btn-submit").click(function() {  

  // get the variable email and put it in a new variable
  var email = $("input#mail").val();   
  var dataString = 'mail='+email;

  $.ajax({
   type: "POST",
   url: "/mail.php",
   data: dataString,
   dataType: "json",
   success: function(msg){

    // JSON return, lets do some magic
    if(msg.status == "ok") { 
     $("#response-box").fadeIn("slow").delay(2000).fadeOut("slow");
     $("#fade").fadeIn("slow").delay(2000).fadeOut("slow");
     $("#response-box .inner").html("<h1>Thank you.</h1>We'll keep in touch!");
     $("#mail").val("e.g. [email protected]");
    } else { 
     $("#response-box").fadeIn("slow").delay(2000).fadeOut("slow");
     $("#fade").fadeIn("slow").delay(2000).fadeOut("slow");
     $("#response-box .inner").html("<h1>Oops.</h1>Please try again!");
    }
   }
  });

  //make sure the form doesn't post
  return false;

 });

});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about optimization