drew.cuthbert
drew.cuthbert

Reputation: 1015

setTimeout not executing the passed function

I'm working on a simple interface for testers to use, which I'm writing as an HTML page. What I need to do is open a specific URL when the user presses a button (the URL triggers a Hudson/Jenkins job on another server). Here is the code I'm using to accomplish this:

function triggerJob() {
  var url = "...";
  var trigger = window.open(url);
  setTimeout(function() {trigger.close();}, 1000);
}

A couple of notes:

Thank you for any help you may be able to give me. I have been toying with this for hours and cannot figure out why my code is not doing the timeout right.

Upvotes: 0

Views: 229

Answers (1)

drew.cuthbert
drew.cuthbert

Reputation: 1015

figured out this problem in the course of doing something else, so I thought I'd share the solution in case anyone else has the same problem. The issue was that I was calling this function using the onClick attribute of a submit button in a form. When the form is submitted it calls the function, and then the page is immediately reloaded after the function executes, which cancels the timeout that was set in the triggerJob function. You must use a link, radio button, etc. if you want to use setTimeout in this manner. Thanks for everyone who tried to help me.

Drew

Upvotes: 1

Related Questions