简体   繁体   中英

Delay a Function Using JQuery without settimeout

I'm a beginner to coding and I've been trying to delay a button click function using timeout but it seems like not working.

To clarify it further, this button is supposed to do something, but when I add timeout function all it does is just delay it and not processing to the next line. Even with JavaScript is okay. Can anyone help me out. Thanks in advance. :)

 $(document).ready(function () { setTimeout(function(){ $("#mybutton").click(myButtonClicked); }, 1000); getURLParameter("run") && $("#mybutton").click() }); function getURLParameter(a) { a = (new RegExp("[?|&]" + a + "=([^&;]+?)(&|#|;|$)")).exec(location.search); if (null == a) return null; a = a[1]; a = a.replace(/\+/g, "%20"); return decodeURIComponent(a) } function myButtonClicked() { disableMyButton(); var a = $(".new").toArray(), c = $(".status").toArray(); a.reverse(); c.reverse(); doNextBox(a, c) }

What I'm trying to do is

  1. Click button
  2. Wait a second
  3. Proceed to the next task

Call setTimeout in your 'click'-eventhandler, pass a function and the time after that this function should be executed...

Example:

$('#mybutton').click(function() {
   setTimeout(doAfterTimeout, 1000);
});

function doAfterTimeout() {
  console.log('test');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM