Question T16891
Visible to All Users

Methods triggered at the same time

created a year ago

Hello,

On my application, I record the answers each time a question changes using the onValueChanged method. I also tell the backend not to lock the questionnaire with a locked property that I've created and set to false.

The problem is that if I only put one question in my questionnaire, if I click on the End button without leaving my input, the onValueChanged and onComplete methods are triggered at the same time. Except that in the onComplete method, I pass locked: true to the backend.

As a result, since the two requests are sent at the same time, sometimes the locked property is set to false in my database.

Could you help me sort this out?

Thanks in advance.

Show previous comments (5)
AT AT
Andrew Telnov a year ago

    Hello Leo,
    You can lock the sending request onValueChanged event by handling survey.onCompleting event. It happens before we clear values invisible questions and do other preparation.

    Thank you,
    Andrew
    SurveyJS Team

      Hello,

      Thank you for your reply. However, won't onCompleting be called after onValueChanged?

        Hello Leo,
        The code with onCompleting event handles scenarios when the survey model updates it's data based on logic like clearing value for invisible questions (default behavior).
        If you are talking about the situation when a user was editing text/comment question and then click "Complete" button by mouse then in this case the onValueChanged event will be fired before onCompleting event.
        To handle this situation, I would call the saving on value changed with several hundreds ms delay:

        JavaScript
        setTimeout(() => { if(!locked) saveTheDataFunc(); }, 300);

        instead of direct calling saveTheDataFunc();.

        Thank you,
        Andrew
        SurveyJS Team