Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Load balancing using setTimeout

I know it's a clickbait title, but this is a serious question. Recently, in Sri Lanka, the government deployed https://fuelpass.gov.lk/ application where a Sri Lankan can register and check the fuel available for the registered vehicle. (The system was designed to control the amount of fuel available to an individual as fuel imports have slowed down due to the economic crisis).

When I checked the system yesterday I realized that they have deployed the dev build to production. As I was going through the code, I noticed that in the Login component, there is a setTimeout call on the form submit to show a loading animation.

The login component looked something like this:

export default () => {
    const [waiting, setWaiting] = useState(false)

    return(
        <Loading showLoading = {waiting}>
            <form onSubmit = {() => {
                setWaiting(true)

                setTimeout(async () => {
                    await loginRequestCall()
                    setWaiting(false)
                }, Math.floor(Math.random() * 5000)
            }}>
                <input />
                <button>Send OTP</button>
            </form>
        </Loading>
    )
}

Screenshot of the source code: enter image description here

The important part is, when user clicks on the Send OTP, It shows the loading screen, only after waiting a random amount of time it sends the request.

I created a video about this and it kinda went viral in Sri Lanka. According to the comments, some of the justifications (for using setTimeout with a random amount of time to wait) were following.

  1. It is a mechanism to prevent DDos attacks
  2. Way to prevent users spamming Send OTP button causing multiple requests
  3. It is a cheap way of load balancing (way to reduce the number of OTP requests)

I wouldn't think if someone wanted to DDos they would be dumb enough to use UI automation. There is no need of setTimeout to prevent spam also.

Which leave me with the last point.

So my questions are,

  1. Is it possible to load balance using setTimeout?
  2. If so, how exactly would it distribute the load?
  3. Is there a valid reason to add a setTimout?
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Is it possible to load balance using setTimeout?

Load balancing is the process of distributing network traffic across multiple servers.

Since load balancing is not reducing the number of calls but distributing them, it is not possible to do load balancing using setTimeout

If so, how exactly would it distribute the load?

As I mentioned, it is impossible to do so with setTimeout, at least not in a non-hacky and inefficient manner.

Is there a valid reason to add a setTimout?

In my opinion, this setTimeout might be intended to be used as a debounce function to avoid the user clicking on the OTP button multiple times if the feedback is not provided immediately (but still not doing the work since all calls will be done anyway).

So the answer is NO, in my opinion, there is not any valid reason for this setTimeout

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!