otp code timer in javascript

You can create an OTP timer in javascript by using the setInterval() function. This function takes two parameters - a callback function which will be executed after a set time interval, and the time interval in milliseconds.

Here's an example code snippet that will generate a 6-digit OTP code every 30 seconds:

index.tsx
let timer = setInterval(() => {
  let otp = Math.floor(100000 + Math.random() * 900000);
  console.log(`Your OTP code is: ${otp}`);
}, 30000);
143 chars
5 lines

In the above code, we're using the Math.random() function to generate a random 6-digit number, and then printing it to the console. The setInterval() function is used to execute this code every 30 seconds.

You can adjust the time interval and the code to generate a different length of OTP code based on your requirements. Also, make sure to clear the timer using clearInterval() once the OTP has been delivered to the user.

gistlibby LogSnag