addEventListener for submit event runs handler function early

后端 未结 1 567
走了就别回头了
走了就别回头了 2021-01-24 06:16

I\'m trying to figure out how to run a chunk of code a few seconds after a user presses a submit button on a form. My test page is waiting the right amount of time and properly

相关标签:
1条回答
  • 2021-01-24 06:42

    Replace

    window.addEventListener('submit', timeFunction(), true);
    

    with

    window.addEventListener('submit', timeFunction, true);
    

    See the difference between invoking a function and referencing it in my other answer.

    Also, instead of doing

    setTimeout("handler()", 3000);
    

    which will basically be eval'd when the timeout occurs, you could pass a reference to the handler function directly to setTimeout. Notice the absence of quotes and the parentheses.

    setTimeout(handler, 3000);
    
    0 讨论(0)
提交回复
热议问题