-1

I have a page with many html forms and I need to go through all of them, submit them and wait for the response, then grab the data.

The natural thing to do is to write a loop like this:

for (i = 0; i < win.document.forms.length; i++)
{
    // Submit form i and wait for the response
}

But then the problem dawns on you: How do you wait for the response inside the loop? You can check whether the data is available, and if not? How do you kill time inside the loop? There is no sleep function in js, right?

To run in a loop and check for the data all the time would suck up all the system resources. You can't do that either. My current wisdom is that you need to exit the loop and terminate, but schedule the containing function for execution later on. When the function runs at some later time you must reenter the loop at the point where you left off and once again check for data available.

It's awkward to say the least. Am I missing something? Is there a better solution?

Henrik3
  • 27
  • 4
  • Of course it's possible to wait. We do that all the time in JS, because lots of stuff are asynchronous. But waiting is non-blocking (unlike PHP). You can use Promises, for instance, or an ajax call with a callback, etc. Look that up – Jeremy Thille Oct 26 '17 at 14:41
  • You should do some research on "asynchronous" operations in Javascript and "callback functions". It's a whole logic to get into, but necessary for a lot of stuff. – Kaddath Oct 26 '17 at 14:42
  • @JeremyThille I know about the asynchronous nature of js, that's the problem. Can you tell me how you would solve the problem with ajax or promises? Please note that no event will be fired when the data become available. – Henrik3 Oct 26 '17 at 14:56
  • @Kaddath I'm painfully aware of that asynchronous nature. How do you solve the problem above? Are you just saying that there's nothing better than my currrent solution? – Henrik3 Oct 26 '17 at 14:58
  • Well that's quite a general question, not a specific problem that can be reproduced, so it doesn't belong here. I believe you need to use `Promise.all([...])` or something, that will wait for several Promises to end – Jeremy Thille Oct 26 '17 at 14:58
  • @JeremyThille It's not a general question, it's the particular problem above. – Henrik3 Oct 26 '17 at 15:05
  • For a precise answer we need a precise question, you should attempt to write some code describing what you precisely want to do, even if it doesn't run. We don't know when you want to do ajax calls, what you want to do with the results, if you want to assemble multiple ajax calls into one single process, etc – Kaddath Oct 26 '17 at 15:06
  • @Henrik it's not a reproductible problem. You're asking for help to develop a feature and write code – Jeremy Thille Oct 26 '17 at 15:08
  • @Kaddath Ajax calls are hardly practical: I would have to collect all the data in the form, call the event handlers etc – Henrik3 Oct 26 '17 at 15:15
  • you don't need to show an exhaustive form processing and all, your ajax call don't really need to be actually working with a valid url. But we must know what type of operation you want to do and when. A basic ajax call like in Ľuboš Čurgó's answer would do. – Kaddath Oct 26 '17 at 15:20

1 Answers1

0

You can use just ajax asynchronous calls with callbacks so you are not blocking thread.

$("form").each(function (index, element) {
    var form = $(element);

    $.ajax({
        url: form.attr('action'),
        type: 'POST',
        data: form.serialize(),
        success: function (result) {
            // ... Process the result ...
        }
    });
});

If you want to use for loop then you will need to also create outer function because variable defined in that loop will not be lexically scoped, so you can end up with submiting same form multiple times.

Ľuboš Pilka
  • 1,044
  • 1
  • 11
  • 17
  • Interesting. You're saying that there is a ready solution in JQuery? How does it handle the form's event handlers? Suppose f.ex. that the form has an onsubmit handler? – Henrik3 Oct 26 '17 at 15:22
  • you need to override that default functionality when submiting like in https://stackoverflow.com/questions/6462143/prevent-default-on-form-submit-jquery – Ľuboš Pilka Oct 26 '17 at 15:29
  • Override? I can't do that because then it won't work. All the js code in the doc must run as usual. – Henrik3 Oct 26 '17 at 16:01