1

I am trying to use javascript on a submit button. Upon hitting the submit button, I run a routine that does a remote callback with callback handler h. Basing on the result of h, I'll decide whether or not to proceed with the submit or to preventDefault and stay on the page.

I tried several approaches. First one is to run the submit using jquery after the callback:

$('#submit').on('click', function(e) {
  e.preventDefault();
  run_func_with_callback(h);
}

h() {
  if (success) {
    $('#submit').submit();
  }
}

Another one is trying for an infinite loop until a flag is changed by the call back:

callback_flag = 0;

$('#submit').on('click', function(e) {
  run_func_with_callback(h);
  while(callback_flag == 0) {
    setTimeout(function() {}, 1000); // I want some form of sleep function here
  }

  if (callback_flag == -1) {
    e.preventDefault();
  }
}

h() {
  if (success) {
    callback_flag = 1;
  } else {
    callback_flag = -1;
  }
}

However, neither of these solutions work. First one I guess jQuery just doesn't really work that way. Second one the method I did was not a sleep function and does not behave in the way I need.

Any insights are welcomed. Thanks!

gtr32x
  • 2,033
  • 4
  • 20
  • 32
  • 2
    The only way is to prevent the event immediately and then trigger it again from within the callback if needed. – Felix Kling Jan 12 '13 at 10:25
  • Your insight is correct - the second method won't work. You need to trigger the submit form the callback. – John Dvorak Jan 12 '13 at 10:26
  • Both of you seem to mention that I should trigger the submit from within the callback as in my first method. However, when I attempt it, the page still freezes and nothing happens. Is there something else I should do or is the syntax different? – gtr32x Jan 12 '13 at 10:28
  • The first approach looks good, with one major detail: what is `success`? I would expect it to be derived from the response, but you don't even _declare_ the response argument to your callback. – John Dvorak Jan 12 '13 at 10:28
  • Try the first method, but .submit() the form, not the button. – darma Jan 12 '13 at 10:29
  • *related* http://stackoverflow.com/q/14241980/218196 – Felix Kling Jan 12 '13 at 10:30
  • @gtr32x: You have to make sure not to end up in an infinite event loop, e.g. by using a flag. I.e. the second time you submit the form, the asynchronous call should not be made and you should not call `.prevenDefault`. – Felix Kling Jan 12 '13 at 10:31

2 Answers2

3

Sticking with your first approach, would something like this work:

$('#submit').on('click', function(e) {
    if(!success) {
        e.preventDefault();
        run_func_with_callback(h);
    }
    else {
        success = false;
    }
}

h() {
    if (success) {
        $('#submit').click();
    }
}

The important thing is to trigger the click event in the callback and then use the value of success to conditionally execute the submission.

nick_w
  • 14,758
  • 3
  • 51
  • 71
0

Can you try this?

$('#submit').on('click', function(e) {
  e.preventDefault();
  run_func_with_callback(h);
}

h() {
  if (success) {
    $('#submit').closest("form").submit();
  }
}
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117