0

I've read through a number of similar question on Stack Overflow, e.g., Is there a [universal] way to invoke a default action after calling event.preventDefault()?, however in this case I do not wish to to prevent the default event, but rather run some additional code as a callback once that default event has started.

Is this possible? A colleague uses a timeout with a value 1, surmising that on the current event loop, the default event will be started run, and on the next loop, the timed event will run. What do you think of this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 1
    Adding `setTimeout(function(){}, 0)` was going to be my suggestion. – gen_Eric Jan 07 '13 at 15:22
  • @RocketHazmat Then add it as an answer if you like. – mikemaccana Jan 07 '13 at 15:25
  • That is what I use as well. There is no clean way to attach a handler that executes after an event has executed/handled/bubbled. – Fabrício Matté Jan 07 '13 at 15:27
  • The drawback of `setTimeout` is that your code loses synchronicity, which makes it harder to execute unit tests. – Fabrício Matté Jan 07 '13 at 15:28
  • Is there a particular reason why you're using the `setTimeout` instead of a direct call? The problem with `setTimeout` is that you don't really know when it's going to happen. If you just want a clean event loop you could look at [postMessage](https://developer.mozilla.org/en-US/docs/DOM/window.postMessage). I think this will fire before the next event loop and doesn't suffer the default event loop timeout. – Halcyon Jan 07 '13 at 15:25
  • Calling something directly would run it immediately, rather than waiting for the default event to fire. – mikemaccana Jan 07 '13 at 15:26
  • It does, but you don't always have to wait for the event to complete. An `onchange` event for instance will have its side effects applied already. `onkeydown`, on the other hand, will not. – Halcyon Jan 07 '13 at 15:30
  • On the docs for [`setTimeout`](https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout), there's a link to a nice example of `postMessage`: http://dbaron.org/log/20100309-faster-timeouts – gen_Eric Jan 07 '13 at 15:34
  • @FritsvanCampen As mentioned in my question, in my case I do need to wait for the default event to start - specifically I wish to disable certain fields immediately after a POST has started (as POST ignores disabled fields by design). – mikemaccana Jan 07 '13 at 15:44
  • So you have an `onsubmit` handler that disables some fields? Keep in mind that `onsubmit` fires _before_ the form is sent to the server. If you were to add a `setTimeout` in the `onsubmit` it will run _after_, by then you're too late to disable any fields. – Halcyon Jan 08 '13 at 12:05
  • @FritsvanCampen Reiterating: I want to disable the fields *after* they are posted. I wish to upload the fields that will be disabled. Later those fields will be re-enabled. I could provide a full written discourse on my rationale, but rather than having to justify the question, I merely seek an answer to it. Hence the title of my question 'JS: fire additional code *after* default?'. – mikemaccana Jan 08 '13 at 14:11

1 Answers1

1

If you want to run code after the default event is finished, then setTimeout is the way to go.

setTimeout(function(){}, 0)

The value of 0ms should push this function to the bottom of the stack, so it'll be ran after the event is done.

Have a look at this answer for a better explanation: https://stackoverflow.com/a/779785/206403

Community
  • 1
  • 1
gen_Eric
  • 223,194
  • 41
  • 299
  • 337