-3

My JS event looks like:

        $(document).on('change', 'input[name="' + ID +'[]"]', function() {
            upload($(this));
        });

So multiple fields can invoke this event, how I can call

upload($(this));

only then if previous event is finished ?

Wizard
  • 10,985
  • 38
  • 91
  • 165
  • what do you mean by event is finished ? – murli2308 Apr 04 '16 at 10:18
  • I mean that when `upload();` is executed by first event only then allow 2 – Wizard Apr 04 '16 at 10:19
  • You should use a queue system to push each event as they are called. Take a look at the `Async` library: https://github.com/caolan/async – cl3m Apr 04 '16 at 10:20
  • it depends on what does the `upload` function do ? It is not a `async` function then you don't have to do any changes it will work as u expected – murli2308 Apr 04 '16 at 10:23
  • @murli2308 idea is not allow execute multiple `upload();` on same time – Wizard Apr 04 '16 at 10:25
  • See: [How can jQuery deferred be used?](http://stackoverflow.com/questions/4869609/how-can-jquery-deferred-be-used?lq=1) – Yogi Apr 04 '16 at 10:39

1 Answers1

1

You could setup a queue management system, using the Async library for example, with something like:

var q = async.queue(function (element, callback) {
    // calling the upload task with the element arguments
    upload(element)
    // calling next task
    callback();
}, 1); // we limit the task to 1 at a time

$(document).on('change', 'input[name="' + ID +'[]"]', function() {
    // enqueing the task with $(this) as argument
    var val = $(this).val()
    q.push($(this), function (err) {
        // this will be logged when the task is completed
        console.log('finished processing ' + val);
    });
});

Another way would be to use an array of Promises

cl3m
  • 2,791
  • 19
  • 21