Avoiding a javascript race condition

后端 未结 7 2124
醉话见心
醉话见心 2020-12-23 12:26

Here\'s the scenario:

My users are presented a grid, basically, a stripped down version of a spreadsheet. There are textboxes in each row in the grid. When they ch

相关标签:
7条回答
  • 2020-12-23 12:35

    Disable the save button during validation. Set it to disabled as the first thing validation does, and re-enable it as it finishes.

    e.g.

    function UserInputChanged(control) {
        // --> disable button here --< 
        currentControl = control;
        // use setTimeout to simulate slow validation code (production code does not use setTimeout)
        setTimeout("ValidateAmount()", 100); 
    }
    

    and

    function ValidateAmount() {
        // various validationey functions here
        amount = currentControl.value; // save value to collection
        document.getElementById("Subtotal").innerHTML = amount; // update subtotals
        // --> enable button here if validation passes --<
    }
    

    You'll have to adjust when you remove the setTimeout and make the validation one function, but unless your users have superhuman reflexes, you should be good to go.

    0 讨论(0)
  • 2020-12-23 12:37

    I think the timeout is causing your problem... if that's going to be plain code (no asynchronous AJAX calls, timeouts etc) then I don't think that SaveForm will be executed before UserInputChanged completes.

    0 讨论(0)
  • 2020-12-23 12:39

    Use the semaphore (let's call it StillNeedsValidating). if the SaveForm function sees the StillNeedsValidating semaphore is up, have it activate a second semaphore of its own (which I'll call FormNeedsSaving here) and return. When the validation function finishes, if the FormNeedsSaving semaphore is up, it calls the SaveForm function on its own.

    In jankcode;

    function UserInputChanged(control) {
        StillNeedsValidating = true;
        // do validation
        StillNeedsValidating = false;
        if (FormNeedsSaving) saveForm(); 
    }
    
    function SaveForm() {
        if (StillNeedsValidating) { FormNeedsSaving=true; return; }
        // call web service to save value
        FormNeedsSaving = false;
    }
    
    0 讨论(0)
  • 2020-12-23 12:42

    A semaphore or mutex is probably the best way to go, but instead of a busy loop, just use a setTimeout() to simulate a thread sleep. Like this:

    busy = false;
    
    function UserInputChanged(control) {
        busy = true;
        currentControl = control;
        // use setTimeout to simulate slow validation code (production code does not use setTimeout)
        setTimeout("ValidateAmount()", 100); 
    }
    
    function SaveForm() {
        if(busy) 
        {
           setTimeout("SaveForm()", 10);
           return;
        }
    
        // call web service to save value
        document.getElementById("SavedAmount").innerHTML = amount;
    }
    
    function ValidateAmount() {
        // various validationey functions here
        amount = currentControl.value; // save value to collection
        document.getElementById("Subtotal").innerHTML = amount; // update subtotals
        busy = false;
    }
    
    0 讨论(0)
  • 2020-12-23 12:52

    You could set up a recurring function that monitors the state of the entire grid and raises an event that indicates whether the entire grid is valid or not.

    Your 'submit form' button would then enable or disable itself based on that status.

    Oh I see a similar response now - that works too, of course.

    0 讨论(0)
  • 2020-12-23 12:52

    When working with async data sources you can certainly have race conditions because the JavaScript process thread continues to execute directives that may depend on the data which has not yet returned from the remote data source. That's why we have callback functions.

    In your example, the call to the validation code needs to have a callback function that can do something when validation returns.

    However, when making something with complicated logic or trying to troubleshoot or enhance an existing series of callbacks, you can go nuts.

    That's the reason I created the proto-q library: http://code.google.com/p/proto-q/

    Check it out if you do a lot of this type of work.

    0 讨论(0)
提交回复
热议问题