Synchronizing Javascript Events

Is there a way to sync events in javascript?

My situation is as follows: I have an input form with many fields, each with an onchange event registered. there is also a button to open a popup for some other / special things to do there.

My requirement is for the onchange events (s) to be finished before I can open the popup.

Any ideas how I can achieve this without using setTimeout?

EDIT: Further explanation of the requirements:

To clarify the situation, I am trying to elaborate on what I am doing.

I have a form with some input elements (order input matrix form, like article, serial number, qty). Every time the user changes data in one of the fields, the ajax call is triggered by the onchange event to validate the user's input and read additional data (for example, presetting / shaping one of the other fields). These ajax calls are heavy and costly, so I must avoid re-checking. There is also a button that opens a popup that gives the user another form to change the data they entered before the start of the line, so it is absolutely imperative that all checks are done before the popup is opened.At the moment I am trying to synchronize the onchange events and the popup opening with setTimeout (the popup doesn’t open until all checks are complete), which is causing problems on my clients site because these popups are trapped in the popup blocker. So I need to open the pop-ups without stopping at some of the pop-up blockers (IE 6/7/8).

Due to my matrix form, I just can't check all the input elements before opening the popup, I only need to check the ones that have changed and not yet confirmed (there should be no more than 1).

0


a source to share


7 replies


It looks like you are doing form validation with an automatic popup when the form is fully completed. To do this, you write a single validation function in javascript that validates every field in the form. You can trigger this function from each of your OnChange events and open a popup when the entire form is validated successfully.

Consider jQuery validation when you're short on time.



http://jquery.com/

+2


a source


you can set up a small callback for your onchange events to ensure that all your checks happen before the popup.

function onChange(callback)
{
    // Do validation

    // Call the callback
    callback();
}

function showPopup()
{
    // Show the popup
}

      



Then on your onchange call just call

onChange(showPopup);

      

+1


a source


If you set a global variable and use setTimeout to check if it is set correctly. Depending on how complicated the situation is, you can use a boolean, two booleans, a number that is incremented, or even an object. Personally, I would like to use the object in a way I know that hasn't been fired yet. sort ofvar isDone = {username: 0, password: 0, password2: 0};

0


a source


Let's assume that input fields only mean text inputs and not any checkboxes or combinations (I'm assuming you are trying to autocomplete). My advice is to use onkeyup and onkeydown.

var keypressed = false;

function onkeydown( )
{
   keypressed = true;
}

function onkeyup( )
{
  keypressed = false;
  setTimeout( function()
              { 
                 if (!keypressed) 
                    show_popup();
                 else 
                    setTimeout( this.calee,1000)
               }, 1000 );
}

      

0


a source


Set flags (variables) for each test group.

  • Initiate flag to 0.
  • Set the flag to 1 when validation is complete for the group.
  • When the user pops the button, if all flags are 1, the popup.

The callback that John talked about will solve the problem "what do you do if they haven't passed validation yet?"


EDIT: Added after clarification:

Have you considered adding a popup button via DOM methods (just) (or innerHTML if you like), after everything is checked? So there is no option until his time.: D

Also do you check if the popup is blocked? If so, you can either send a notification to the user that their blocker is blocking the editor; or load your editor into an iframe automatically; or to load the editor to the main page using DOM methods (adding documentFragment, etc.).

Some blockers provide users with the ability to block even pop-ups created by clicking on links (which are traditionally not limited to blockers). I would think some kind of backup method would be helpful to you, or at least an in-place warning system.

NTN

0


a source


I don't think I fully understood your question, but here are some thoughts on how to solve problems you might have :)

firstly, i deactivate the popup open button when sending the ajax call. then when the requested data comes in and the whole check is done, activate it again. you can do this with a counter: increase it for each request sent, decrease it as soon as the data arrives and the validation is complete. activate popup open when data arrives and the counter is zero. this prevents the user from clicking the pop-up button while validation requests are not yet pending.

you can use the same method for the input fields themselves: lock the input fields that are pending validation by setting them to read, unlock them when done.

to prevent problems when the user changes form values ​​while the ajax call hasn't returned yet, you have several options:

  • use a timer to send the request: every time the onchange event fires, wait x seconds before sending the request. if another exchange event occurs before the ajax request is sent, reset this timer. this way multiple exchange events with a specific timeframe only fire one ajax request. this helps to reduce the load.

  • you can calculate and store checksums for each position, so if an onchange event occurs, calculate the checksums again and compare them. this way you know which parts have actually changed, avoiding unnecessary validation prompts.

too, never place your bets on time (if I understand correctly what is needed for the setup). x seconds may be sufficient under normal circumstances, but in the worst case ...

0


a source


We needed something like this for a wizard where some steps require AJAX validation. The user will not be allowed to close the wizard by clicking Finish if there are any pending checks. To do this, we just had a pending validation counter and a flag to signal if the user wanted to close the wizard. The main algorithm was:

  • If a new AJAX check is in progress, increase the number of peers.
  • Decrease the pending count when the AJAX confirmation returns.
  • If the pending count reaches zero when decreasing, check the "finish" flag; if installed, complete the wizard.
  • When the user clicks the "Finish" button, check the "pending" invoice; if it is zero, exit the wizard; it is not zero, set the "finish" flag.

Thus, synchronization can only be handled with two variables ("waiting", "finish").

I highly recommend using multiple flags for every single AJAX operation; a state machine usually gets out of control when states are tracked by multiple state variables. Try to avoid this if absolutely necessary.

I also don't suggest using it setTimeout

to randomly wait until the required conditions are met. With the above counter, your code will act on conditions change as soon as they change.

0


a source







All Articles