JavaScript JavaScript/jQuery callback using Ajax javascript jquery callback,javascript jquery callback function,javascript callback jquery

You've almost got it!

The same way that you call getMovieInfo in the success callback of getMovieList, you should be calling countTheMovies in the success callback of getMovieInfo.

Answer:1

As Jacob said above, move the countTheMovies call inside the AJAX request.

$(function(){
  getMovieList();
});

function getMovieList() {
  $.ajax({
    url: "movielist.json",
    type: "GET",
    dataType: "JSON",
    success: function(data) {
      for (var i = 0; i < data.length; i++) {
        var title = data[i].title.toLowerCase().split(" ").join("+");
        var year = data[i].year;
        i === data.length - 1 
          ? getMovieInfo(title, year, true) 
          : getMovieInfo(title, year, false);
      }
    }
  });
}

function getMovieInfo(title, year, isLast) {
  $.ajax({
    url: "https://www.omdbapi.com/?t=" + title + "&y=" + year + "&plot=short&r=json",
    type: "GET",
    crossDomain: true,
    dataType: "JSON",
    success: function(val) {
      if (!val.Error) {
        movie = title.replace(/[^a-z0-9\s]/gi, '');
        $("#app").append(
          // appending info to divs
        );
        if (isLast) countTheMovies();
      }
    }
  });
};

function countTheMovies() {
  $("#app").append("There are " + $(".movie").length + " movies.");
}
Answer:2

You can call your countTheMovies() function from inside the success field of your Ajax call. This way it will make the function call when you intend it to.

Answer:3

I have a frameset and 2 frames. What I want to achieve is: A user clicks on an image/button. This opens a page in another frame. I can then call a function within that page. Below is the sample code:...

I have a frameset and 2 frames. What I want to achieve is: A user clicks on an image/button. This opens a page in another frame. I can then call a function within that page. Below is the sample code:...

I tried the code in here Scrolling page in RSelenium but it's not working as the web site that I'm working on will load more content as you scroll down. For example webElem <- remDr$findElement("...

I tried the code in here Scrolling page in RSelenium but it's not working as the web site that I'm working on will load more content as you scroll down. For example webElem <- remDr$findElement("...

  1. selenium scroll down python
  2. selenium scroll down java
  3. selenium scroll down to element
  4. selenium scroll down c#
  5. selenium scroll down javascript
  6. selenium scroll down to element python
  7. selenium scroll down to bottom of page
  8. selenium scroll down in dropdown
  9. selenium scroll down until end
  10. selenium scroll down not working
  11. selenium scroll down page c#
  12. selenium scroll down div python
  13. selenium scroll down pixels
  14. selenium scroll down in div
  15. selenium scroll down to bottom of page c#
  16. selenium scroll down table
  17. selenium scroll down table python
  18. selenium scroll down ruby
  19. selenium scroll down browser
  20. selenium scroll down slowly

I have a form where the user enters some numbers. These numbers are indexes. The problem is that when I'm posting the model to the server I need the index to be zero-based. But the index should be ...

I have a form where the user enters some numbers. These numbers are indexes. The problem is that when I'm posting the model to the server I need the index to be zero-based. But the index should be ...

I have a really strange problem: I am using jQuery v11 on the latest Chrome on localhost. While I manage to use jQuery.animate() on my website with any elements and features (including opacity), I ...

I have a really strange problem: I am using jQuery v11 on the latest Chrome on localhost. While I manage to use jQuery.animate() on my website with any elements and features (including opacity), I ...

  1. jquery opacity animation example
  2. jquery opacity animation
  3. jquery animation opacity 0 to 1
  4. jquery alpha animation
  5. jquery animate opacity not working
  6. jquery animate opacity toggle
  7. jquery animate opacity 0
  8. jquery animate opacity to 1
  9. jquery animate opacity display none
  10. jquery animate opacity left to right
  11. jquery animate opacity loop
  12. jquery animate opacity on scroll
  13. jquery animate opacity then hide
  14. jquery animate opacity easing
  15. jquery animate opacity slow
  16. jquery animate opacity w3schools
  17. jquery animate opacity time
  18. jquery animate opacity doesn't work
  19. jquery animate opacity display block
  20. jquery animate opacity 0 to 100