Berglund
Berglund

Reputation: 676

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

Upvotes: 1

Views: 225

Answers (1)

Tommy Brettschneider
Tommy Brettschneider

Reputation: 1490

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');
}

Upvotes: 5

Related Questions