0

I made a function that executes data by requesting data whose id is i.

I want i = 1 -> run() and i = 2 -> run() -> ... i = 10 -> run()

After the first run, the second request and the second run...

However, the result is that ten getJSON requests are executed first .

How do I get the results I want?

Below is the code I wrote.

for(i=0;i<10;i++){

  $.getJOSN('a',{id:i},function(data){
      collectInfo = JSON.parse(data);
  }
  run();
}
  • 1
    Even though you *can*, you probably shouldn't - learning to use asynchronousness is a core component of JS. – CertainPerformance May 09 '18 at 01:02
  • @CertainPerformance Is there any way to synchronize getJSON? –  May 09 '18 at 01:58
  • You really shouldn't use it. In fact, if you do, then some web browsers will display a warning in the console window about using it. This can cause performance problems and will ruin the user experience. There are many tutorials out there about how to use $.getJSON and similar in an asynchronous fashion. – Andrew May 09 '18 at 04:11
  • There is another issue with your code. You're running it inside the loop which will also be bad for performance. You make a request 10 times. Instead, you should find a way to call it one time outside of the loop and get all of the data. – Andrew May 09 '18 at 04:12
  • `Is there any way to synchronize getJSON` Please read the linked question – CertainPerformance May 09 '18 at 04:27

0 Answers0