save quiz time

Asked

Viewed 41 times

1

I did a survey with php and javascript, it’s all right. There’s just one small detail that I’m not sure which is the best way to do it.

The questionnaire has a time to be answered, and I would like that if the responding user leaves the page or update, the time starts to tell where he left off. I thought about making a request by writing to the comic every 2s the running time, but I would like to avoid something that would make so many requests to the database in such a short time, even more that the system is in Amazon, I want to avoid to generate additional costs.

I thought of other ways, such as Sesssions and cookies but either one is not efficient for this.

If you can help me I’m very grateful, I just need a hint as to what your experience would be a more viable option.

  • It is possible to confirm the exit of the page, there in this confirmation you save...

  • Yes, but not all browsers that onbeforeunload work very well. But thank you

1 answer

2

You can use the localStorage, provides access to local storage for a specific domain, allowing you, for example, to add, modify, or delete stored data items.

Saving in the localStorage time and an identification for the user:

storage.setItem(usuario, tempo);

Reading the value:

var tempo = storage.getItem(usuario);

You can use this because the data stored on localStorage don’t expire.

More information: https://developer.mozilla.org/en-US/docs/Web/API/Storage

  • i even thought of using localStorage But the user can’t just erase this by wiping browsing data? How is it done with cookies, caching, etc.? This would be a problem given that user could cheat time this way.

  • 1

    The problem is that the user can delete this record easily and answer the questionnaire with all the time in the world

  • 2

    I suggest you perform with onunload and onbeforeunload and test in all possible browsers. Those who do not find the corresponding event, disable the questionnaire resolution.

  • @Marcusdacorréggio I’ve been researching the events onbeforeunload and onunload and I’ve seen many reports that using these events to trigger for example ajax requests can be a problem since browsers pause everything when trying to close a tab, not to mention the crashes and they don’t get sent. For these reasons I have chosen to try other alternating, but what I am seeing will be my only way out for now kkkk. Thank you very much

  • You can record in the bank at longer intervals to decrease the load, and still use the localStorage, If there is no local information there you search from the bank, the location would be more accurate information of the time, the bank would be a security against those who try to clean the data to circumvent that time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.