R
R
Roman Rakzin2015-07-04 02:20:06
css
Roman Rakzin, 2015-07-04 02:20:06

Triggering a function when all data is loaded?

There are several functions, each of which makes an Ajax request.
Then you need to run a function that will work with the loaded data.
But how can I run this function when the data is already loaded?
The timer is somehow a crutch to put. Alternatively, in the return value of the ajax function request a1, I run the function a2, and so on. many times. As a result, I also get confused as to what goes where.
Is there a more normal option to run the function when all the necessary data has already been loaded? (in Angularjs / jquery or just in javascript)

Answer the question

In order to leave comments, you need to log in

9 answer(s)
H
holfza, 2018-10-24
@greenfox07

jsfiddle.net/cs7w456k/2

C
Conan the Barbarian, 2018-10-24
@JaredWinter

jsfiddle.net/cnL8bjue

M
Maxim Timofeev, 2018-10-24
@webinar

This is clearly a list, so it can be a trite picture(s) or svg. If you make it a widget and programmatically generate items, then svg will be more convenient.

B
Basitkhan Mansurov, 2018-10-24
@Basitkhan

-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
border-top: 1px solid rgba(0,0,0,0.2)
(And don't fuck with box-shadow)

S
Sergey, 2015-07-04
@TwoRS

Use promises (the $http service through which you must make all ajax requests always returns them) and $q.all to wait for all operations to complete.

function foo() {
    return $http.get('/foo');
}

function bar() {
    return $http.get('/bar');
}

$q.all([
  foo(),
  bar()
]).then(function (results) {
   // данные загружены
})

T
Think With Your Head, 2015-07-04
@Vyad

The problem is that Ajax is an asynchronous technology. That is, at first, ordinary js works almost instantly, and Ajax gives the return line later, because of this, all work with Ajax data should take place inside the callback functions, there seems to be no other way.
I don’t know about Angular, maybe there is a beautiful solution, but judging by the principle of operation of Ajax (asynchrony), it’s unlikely

D
D', 2015-07-04
@Denormalization

https://github.com/caolan/async this will help. We look at the Control Flow section and choose what is more suitable for the task (I think parallel will do here ).

A
Alexander, 2015-07-04
@liff

use deferred - this is what you need
1) api.jquery.com/category/deferred-object
2) habrahabr.ru/post/113073

N
Nikita, 2015-07-04
@jetu

deferred , promise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question