Search code examples
javascriptsubmitdelay

Javascript: Delayed submit is not working correctly


https://jsfiddle.net/z1ctm0hd/

JSFiddle generates a strange list of errors (pic http://i.share.pho.to/7e8051a3_l.png), however, the auto-submit is working on my page, but without the requested the delay. Thx anyone for help.

<form id='formBlokUziv' action='' method='post'>

  <div class='onoffswitch'>
        <input type='checkbox' name='onoffswitch1' value='1'   onchange='setTimeout(document.getElementById("formBlokUziv").submit(), 5000);' class='onoffswitch-checkbox' id='myonoffswitch1' >
    <label class='onoffswitch-label' for='myonoffswitch1'>
        <span class='onoffswitch-inner'></span>
        <span class='onoffswitch-switch'></span>
    </label>
  </div>

  <div class='onoffswitch'>
        <input type='checkbox' name='onoffswitch2' value='2' onchange='setTimeout(document.getElementById("formBlokUziv").submit(), 5000);' class='onoffswitch-checkbox' id='myonoffswitch2' >
    <label class='onoffswitch-label' for='myonoffswitch2'>
        <span class='onoffswitch-inner'></span>
        <span class='onoffswitch-switch'></span>
    </label>
  </div>

  <div class='onoffswitch'>
        <input type='checkbox' name='onoffswitch3' value='3' onchange='setTimeout(document.getElementById("formBlokUziv").submit(), 5000);' class='onoffswitch-checkbox' id='myonoffswitch3' >
    <label class='onoffswitch-label' for='myonoffswitch3'>
        <span class='onoffswitch-inner'></span>
        <span class='onoffswitch-switch'></span>
    </label>
  </div>

</form>

Solution

  • Commands entered as the first parameter in a setTimeout call will be executed immediately. Put each of your timeout into a function and it will run after the specified timeout. Something like -

    setTimeout(function() { document.getElementById("formBlokUziv").submit() }, 5000);