tags:

views:

88

answers:

4
$(".item").each(function(){
    var item_link = $(this).find("a").attr("href");
    $(this).prepend('<div class="img_url"></div>');
    var img_url = $('div.img_url', this);
    $.get(item_link, function(data) {
        var src = $('.poster img', data).attr('src');
        img_url.html(src);
    });
});

Each .get should be started after the previous is finished. Now all the .get start in one time.

Any idea?

A: 

You need to use $.ajax() and set async to false.

In this case, you would write:

$.ajax({
    url: itemlink,
    async: false,
    success: function(data) {
       //your success function
    });

or something like that.

Skilldrick
Easy solution, but will hang the browser during execution. Look into `queue` s for an asynchronous solution that is also sequenced.
MvanGeest
can you describe, how to use it? Tryed to replace .get with .ajax - doesn't work
Happy
thanks, but holding the page is not good
Happy
-1: Synchronous calls may "work" but they are **not** a good solution to solve the problem. Event blocking, poor practice.
gnarf
+2  A: 

jQuery is not threaded by default, but I've written plugins that provide functionality that emulates it. There are two parts to the code. The first creates 'Work Queues' and provides functionality for both a foreground and a background Queue where foreground work is prioritized over background work. It's available in this post: http://code.ghostdev.com/posts/javascript-threading-revisted-jquery. The second creates an AJAX Queue which takes priority over the foreground and background Queues and suspends operation while an AJAX call is processing. It's available in this post: http://code.ghostdev.com/posts/javascript-threading-ajax-queueing.

Using them will absolutely require at least some restructuring of your code, but I've implemented them so that I can avoid browser timeouts and schedule things.

Sorry, I can try to include a bit of an example. Assuming you've included those snippets and now have working queues, something like this probably comes close with your example code:

$.addajaxwork('imageLoading', $('.item'), function () {
  var item_link = $(this).find("a").attr("href");

  $(this).prepend('<div class="img_url"></div>');

  var img_url = $('div.img_url', this);

  $.get(item_link, function(data) {
    var src = $('.poster img', data).attr('src');
    img_url.html(src);
  });
});
g.d.d.c
Thats all good, but I'm newbie in javascript so I can't get code working.
Happy
+1  A: 

You could use jQuery's queueing mechanism:

var queueable = $(".item");

queueable.each(function(){

    var item_link = $(this).find("a").attr("href");
    $(this).prepend('<div class="img_url"></div>');
    var img_url = $('div.img_url', this);

    $.queue(queueable, 'get', function(next){
        $.get(item_link, function(data) {
            var src = $('.poster img', data).attr('src');
            img_url.html(src);
            next();
        });
    });

});

$.dequeue(queueable, 'get');
J-P
thanks for the solution, but it doesn't work.
Happy
<div class="img_url"/> creates only for the first .item
Happy
Assuming there is more than one `.item` matched in `queueable`, this is creating a multiple 'animation' queues (on each item in `queueable`) which contain multiple ajax requests (one for each item in `queueable`)
gnarf
@gnarf, Nope. `queueable` is what the queue is being tied to (as in the jQuery instance itself) -- not the elements within. Check the source for `jQuery.queue`. And they're not "animation" queues.
J-P
@Happy, have you checked your error console?
J-P
+2  A: 

You could use this $.ajaxQueue() originally posted on my answer to Sequencing Ajax Requests

(function($) {
  // jQuery on an empty object, we are going to use this as our Queue
  var ajaxQueue = $({});

  $.ajaxQueue = function(ajaxOpts) {
    // hold the original complete function
    var oldComplete = ajaxOpts.complete;

    // queue our ajax request
    ajaxQueue.queue(function(next) {

      // create a complete callback to fire the next event in the queue
      ajaxOpts.complete = function() {
        // fire the original complete if it was there
        if (oldComplete) oldComplete.apply(this, arguments);

        next(); // run the next query in the queue
      };

      // run the query
      $.ajax(ajaxOpts);
    });
  };

})(jQuery);

Applied to your source

$(".item").each(function(){
    var item_link = $(this).find("a").attr("href");
    $(this).prepend('<div class="img_url"></div>');
    var img_url = $('div.img_url', this);
    $.ajaxQueue({
     url: item_link, 
     type: 'GET',
     success: function(data) {
        var src = $('.poster img', data).attr('src');
        img_url.html(src);
     }
    });
});
gnarf
this is perfect, god bless you.
Happy