Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Automatic marking bell Javascript

I’m trying to make an automatic marking bell, using a timer with an array data source, but when run the script only displays one time according to the array source data that I created. The following is the script that I made

function start_bell(){
        var bell_scedule = ['18:52:00','18:53:00','18:54:00','18:55:00','18:56:00'];
        var index = 0;
        window.setInterval(
            function(){
                var d = new Date();
                var s = d.getSeconds();
                var m = d.getMinutes();
                var h = d.getHours();
                var realtime =("0" + h).substr(-2) + ":" + ("0" + m).substr(-2) + ":" + ("0" + s).substr(-2);
                var timer=realtime;
                if(bell_scedule[index]==timer){
                    alert('play');
                }else{
                    console.log('pause');
                }
                index++;
                if(index >= bell_scedule.length){
                    index = 0;
                }
            },1000
        );
}

Hopefully friends who have better knowledge can help me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of incrementing the index each second, we loop through the bell_schedule array every second, comparing each time with the current time (realtime).

If a match is found, we trigger the alert and break the loop for that second to avoid multiple alerts in case there are duplicates in the array.

The index variable is removed because we’re now checking all the scheduled times every second, so there’s no risk of skipping any entries.

function start_bell() {
    var bell_schedule = ['18:52:00', '18:53:00', '18:54:00', '18:55:00', '18:56:00'];

    window.setInterval(function () {
        var d = new Date();
        var s = d.getSeconds();
        var m = d.getMinutes();
        var h = d.getHours();
        var realtime = ("0" + h).substr(-2) + ":" + ("0" + m).substr(-2) + ":" + ("0" + s).substr(-2);
        
        console.log(realtime); // To check the current time

        // Check each schedule time against the current time
        for (var i = 0; i < bell_schedule.length; i++) {
            if (bell_schedule[i] === realtime) {
                alert('play');
                break; // Break the loop if a match is found
            }
        }
    }, 1000);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading