Skip to content

Instantly share code, notes, and snippets.

@thecfguy
Created August 18, 2013 05:54
Show Gist options
  • Save thecfguy/6260114 to your computer and use it in GitHub Desktop.
Save thecfguy/6260114 to your computer and use it in GitHub Desktop.
Wrong way to call ajax within loop and using index in success method.
<html>
<head>
<script src="/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script>
function loadMe(){
for(var i=1;i<=3;i++){
$.ajax({
url:"scribble2.cfm?",
success:function(res){
showMessage(res,i); // Call another function with additional parameter
}
});
}
}
var showMessage = function(res,index){
$("#div" + index).html(res);
}
</script>
<style>div { height:40px; border:1px solid #000;margin: 5px 5px 5px 5px;}</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<a href="javascript:loadMe()">Load Message</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment