1

I have a setup where divs that contain similar data (but ranked differently) need to dynamically load data on start up. To do this I load a PHP page through AJAX and pass a parameter so it knows what rank to query. However in order to load on start I need to call the same function 4 times in a row. Is this syntax correct? Or is there a way to write this without a big list of the same function being called

$(document).ready(function(){
    getStuff(1);
    getStuff(2);
    getStuff(3);
    getStuff(4);
});
function getStuff(type) {
    $.ajax({
       type: "GET"
       ..........
       success: function(html) {
          $('[data-id="' + type + '"]').html(html);
       }
    });
}


<div id="rank1" data-id="1"></div>
<div id="rank2" data-id="2"></div>
<div id="rank3" data-id="3"></div>
<div id="rank4" data-id="4"></div>
3
  • 1
    there should be $('[data-id="' + type+ '"]') Commented Aug 19, 2014 at 14:31
  • Don't know how your PHP works but can't you pass ranks as an array? Then PHP loops through array and creates one div for each rank. Commented Aug 19, 2014 at 14:33
  • there are multiple articles in each div, this div is a container so the database can have like 7 under id 1, 4 under id 2 etc etc.... so thats why i need to query for each rank and spit out the data Commented Aug 19, 2014 at 14:40

4 Answers 4

2
$(document).ready(function(){

  // edit as per taseenb@ comment for performance
  var size = $("div[id^=rank]").size()

  for(var i = 1; i <= size; i++){
    getStuff(i);
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

just a little performance and good practice bonus comment ;) i would not use a jquery selector in a for loop. save $("div[id^=rank]").size() into a variable before using it. don't make the browser run jquery selectors for no reason.
@taseenb Agreed. Fixed it. I normally do it that way but I wanted to win the Answers race here.
1

Of course there is a way and it is called for loop :)

$(document).ready(function(){
    for(i = 1; i < 5; i++){  
       getStuff(i);
    }
});

Comments

1

Why don't you make getStuff take an array and make the AJAX calls in a loop?

Also, something to consider: AJAX is asynchronous (as implied by the name), so there's no guarantee that these calls will return in the same order you make them. If you need them to happen sequentially, you should make the calls recursively--make each subsequent call from the SUCCESS function of the previous call. That way you guarantee that the previous call has finished before you make the next call.

4 Comments

Or just add async : false to ajax
NEVER do async : false, never.
What is bad about async : false?
You freeze the browser UI. And you lose all the benefits of using ajax basically.
1

Select via [attribute] selector, iterate via $.each():

$('[data-id]').each(function(){

  var $this = $(this);

  $.get( '/some/url' {
     ..........
  }).done(function(html){

    $this.html(html);

  });  

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.