Frage

Im Grunde genommen in meiner Website habe ich eine Sidebar mit einem Stapel von Boxen; Jeder Kasten kann durch den Benutzer reduziert oder erweitert werden. Ich brauche den Status jedes Feld für den aktuell angemeldeten Benutzers zu speichern. Ich möchte nicht, Cookies verwenden, weil, wenn ein Benutzer Browser oder Computer ändert, werden alle Boxen werden auf den Default-Status gehen.

speichern soll ich diese Informationen auf der Datenbank eine Abfrage machen jedes Mal der Benutzer kollabiert / eine Box erweitert? Wie würden Sie damit umgehen? Danke.

Edit: Was passiert, wenn ein Benutzer klickt auf der Toggle-Taste so oft, wie zehnmal in zwei Sekunden, nur weil er die Boxen Animation genießt? Er wird zehn Abfragen in zwei Sekunden machen. Vielleicht sollte die Frage sein: , wenn sollte ich speichern diese Daten

War es hilfreich?

Lösung

  1. Call a (client-side) "changed" function every time a box changes.

  2. Keep two items of (client-side) state: the time the last update to the server was sent and whether a timer has been set.

  3. Write a (client-side) "update" function that sends the update and updates the state to mark that the last update was just now.

  4. When the changed function is called: if a timer is set, then return immediately; if an update has never been sent or the last update was sent more than ten seconds ago, then call the update function and return. Otherwise set a timer to send the update after ten seconds.

  5. The timer callback should simply clear the timer flag and call the update function.

  6. Also on an unload event check if a timer was set and if it was then clear the timer and call the timer callback function.

So the result is that you send the update immediately except when the user is flapping, in which case you only send an update every ten seconds at most.

There might be some cases where you lose an update, but this is likely to only happen if the user was playing with the toggling and then closed the page before the timer fired, in which case he probably won't notice anyway.

Andere Tipps

If you need to persist these options from multiple computers you will need some form of server side storage.

This could be database or flat file. The choice depends on what you have available, skill set, and the need to scale. If you are going to have just a few users, a flat file may be your best choice.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top