Link to home
Start Free TrialLog in
Avatar of blueskybooking
blueskybookingFlag for Canada

asked on

Waiting for an field value to be updated from a spawned postMessage receive event during a click event

I want to pause during a click event, waiting for a postMessage to return and then continue with the rest of the code in the click event.

My question is how to synchronously "sleep" while the code awaits the returned response message. Adding a timeout (in case of failure) would be a bonus.

My "WaitForResponse" function is not working properly.

https://jsfiddle.net/blueskybooking/zagc18fd/16/

User generated image

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey there,

Your receiveMessage function fires once a message comes in, so just put your code in there. Fre off postMessage in the click, and move everything after that to the receiveMessage function.
Avatar of blueskybooking

ASKER

I was looking to simplify the code but I think that created confusion.

Try this version, it may illustrate the problem better. Just click Start Test before 10 seconds.

https://jsfiddle.net/blueskybooking/zagc18fd/19/
Hmmm. Still a little confused. You have an event handler that get's fired when your page receives a message, so there's no need to 'wait' for anything. Just wrap your code in that function - it will only fire once you get a message. Am I missing something ?
I've tried to simplify what I'm trying to accomplish using jsFiddle.

In my live site, the flow that I'm looking for is:

  1. User clicks Submit button.
  2. Click event activates.
  3. Depending on the scenario, a post is triggered through a 3rd party iframe (in this case a credit card processor).
  4. When that external post completes, postMessage receives a message back from that 3rd party iframe.
  5. I want to wait for for that "returned message" while inside the click event.
  6. Once the returned message is received, I want to continue with the click event code which will end up submitting my entire form.

Let me know if that clarifies things. Thank you.
Yeah - that makes sense, but my point is that any code you want to run after the message comes back should go in the messageRecevied callback. So your click event triggers the postMessage. That's it. Then, when the message is recevied back, your callback is fired and you can do whatever you need. No need to wait around.

Have a look at this fiddle to see if it makes more sense - https://jsfiddle.net/ChrisStanyon/pz5mjg0s/
I've got some rather complicated code and I found I can't trigger a submit through the received event.

You can get an idea from here:
https://jsfiddle.net/blueskybooking/uqfr35s0/4/

If you click on "C" you'll re-direct to the link as expected. So the default action is preserved. However, if you click "A" or "B" it will not run this default action.

Since I'm using ASP.NET WebForms, working through the #__EVENTTARGET AND #__EVENTARGUMENT is not ideal. Which is what I'd probably have to do (to make sure the correct button was transmitted to the server) if I did it through the received event.

That's why I'm hopeful for a "sleep" cycle which can keep everything within the single click event.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your help gave me the clues I needed to resolve my issue, it turns out I had unbound the click event and then called it subsequently in the receiveMessage event in my live code. Thus it wasn't working as expected.

Your help and code fully addresses the click event I outlined here and deserves 5 stars. Thank you.