Avoiding a javascript race condition

后端 未结 7 2235
醉话见心
醉话见心 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条回答
  •  梦毁少年i
    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;
    }
    

提交回复
热议问题