Timer in LED using JS

Hello,
I want to add a timer on my LED connected to Bolt to turn on and off every 5 seconds but unable to do so. I have tried the setTimeout and setInterval method in JS but it’s not working as expected. Also i have gone through the threads in this forum but not getting any idea. I think i am missing something. Please help. Below is the code.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>LED Control</title>
    <script type="text/javascript" src="scripts.js"></script>
    <script type="text/javascript">setKey('API','Board-Name');</script>
    <script>
        function startTimer() {
            for(var i = 0; i < 2; i++) {
                setTimeout(() => {
                    digitalWrite(0, 'HIGH');
                }, 4000);
                setTimeout(() => {
                    digitalWrite(0, 'LOW');
                }, 4000);
            }
        }
    </script>
</head>
<body>
    <button style="background-color: greenyellow;" onclick="startTimer()">LED ON</button>
    <button style="background-color: red;" onclick="digitalWrite(0, 'LOW')">LED OFF</button>
</body>
</html>

@bsinghbharats Do you want to achieve blinking of LED at an interval of 5 seconds on button click?

Yes. Can you help me @shobhit.kumawat ?

Here is the code of blinking led. Hope it will help you

<!DOCTYPE html>
<html>
    <head>
        <title>Bolt IoT Platform</title>
        <script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
        <script>
        setKey('{{ApiKey}}','{{Name}}');
        </script>
        <script>
        function myFunction1() {
            digitalWrite(0, 'HIGH');
            setTimeout(myFunction2, 300);
         }
         function myFunction2() {
            digitalWrite(0, 'LOW');
            setTimeout(myFunction1, 300);
         }
        </script>
    </head>
    <body>
        <center>
        <button onclick="setTimeout(myFunction1, 300);">ON</button>
        <button onclick="digitalWrite(0, 'LOW');">OFF</button>
        </center>
    </body>
</html>
1 Like

setTimeout() and setInterval() functions allow you to execute a piece of JavaScript code/function at a certain point in the future. setInterval repeats the call, setTimeout only runs it once.

setTimeout(expression, timeout); runs the code/function once after the timeout. It is a time based code execution method that will execute script only one time when the interval is reached, and not repeat

again unless you gear it to loop the script by nesting the setTimeout object inside of the function it calls to run. If geared to loop, it will keep firing at the interval unless you call clearTimeout(). If you want

something to happen one time after some seconds Then use setTimeout… because it only executes one time when the interval is reached.

setTimeout(function() {
  console.log('Wait 3 seconds and I appear just once');
}, 3000);

setInterval(expression, timeout); runs the code/function repeatedly, with the length of the timeout between each repeat. It is a time interval based code execution method that has the native ability to repeatedly

run specified script when the interval is reached. It should not be nested into its callback function by the script author to make it loop, since it loops by default. It will keep firing at the interval unless you call

clearInterval(). If you want to loop code for animations or clocks Then use setInterval.

setInterval(function() {
  console.log('Every 3 seconds I appear on your console');
}, 3000)