Created by @e2221 in Q&As
@e2221
@e2221

Hello, I have a question how can I implement auto-submit form but with setTimeout()? Could you please help me?

@e2221
@e2221

I need to call some function in

$(document).on('keyup', 'input[data-autosubmit]', function(e){
    ...get form
    someFunction(form, submitBy);
}

someFunction = function(form, submitBy){

}

I can find this post https://forum.nette.org/cs/25506-nittro-novinka-pro-vas-frontend-puvodne-nettejs#p189072 but canĀ“t make it works with form :(

@jahudka
@jahudka

Hi @e2221, to submit a form you can do this:

<form n:name="myForm"> <!-- ... --> </form>

<script type="application/javascript">
_stack.push(function(di) {
  var tmr = null;

  // any html element can be a snippet from Nittro's point of view,
  // so why not the form?
  di.getService('page').getSnippet({form.id myForm})
    .setup(function(frm) {
      $(frm).on('keyup', 'input[data-autosubmit]', function() {
        tmr && window.clearTimeout(tmr);
        tmr = window.setTimeout(function() {
          // this is how you submit it:
          di.getService('formLocator').getForm(frm).submit();
        }, 2000);
      });
    })
    .teardown(function() {
      tmr && window.clearTimeout(tmr);
      tmr = null;
    });
});
</script>

Notice I'm not attaching the keyup handler to document, but rather to the form - that way the handler is automatically discarded by the browser when the form itself disappears from the page, which is what you want.

@e2221
@e2221

@jahudka Yes! Thanks for your time.

Sign in to post a reply