N
N
Nick Mock2021-09-06 00:19:12
Node.js
Nick Mock, 2021-09-06 00:19:12

How to limit the number of requests after multiple clicks on a button?

I am writing a bot on nodejs for telegram, I ran into such a problem that when a user is saved in the database , it is saved in several instances, provided that he repeatedly pressed the inline button that is responsible for saving users to the database , well, or the bot was idle for a couple of minutes, but at startup, it starts processing incoming requests, and also saves users in several instances. How to solve this problem in general, where to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Roganov, 2021-09-06
@Nurked

First, what you are looking for is called throttle. If you google throttle js, you can find many possible solutions.
https://stackoverflow.com/questions/27078285/simpl...
Secondly, after the first click on the button, it must be blocked (and if this cannot be done on the screen, then it will need to be blocked in the code by setting the isBlocked variable to true).
While the button is disabled, repeated requests are simply ignored. When the first request has completed, unlock the button.

S
SKEPTIC, 2021-09-06
@pro100chel

The simplest thing that can be done is to enter the user's id into a list before making a request to the database. And after request in basis - to delete from the list. When registering, check the presence of the user id in this list. If it's there, we don't do anything.
======
Another way. Move the user registration process to a separate thread. This will allow you to serve requests for registration in order of priority and avoid duplicates in the database.
=======
Another way. Put a unique index on the user id in the database. It at the level of a DBMS will not allow to make a double.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question