I am building an Azan clock with Javascript with the following code, but the code does not launch the mp3 file when the time from the API and my PC match.
I am already converting the system time to string time to match with the API but it is kind of ignored by the console.
Screenshot of time format in console
My code is below
// Enable JQuery
var script = document.createElement("script");
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js";
document.head.appendChild(script);
// Call and Parse the API
var request = new XMLHttpRequest();
request.open(
"GET",
"https://api.aladhan.com/v1/timingsByCity?city=AbuDhabi&country=uae&method=8",
false
); // `false` makes the request synchronous
request.send(null);
if (request.status === 200) {
var azan = JSON.parse(request.responseText);
}
// Convert system time format
var convertFormat = new Date().toLocaleTimeString("en-US", {
hour: "numeric",
hour12: false,
minute: "numeric",
});
// Play the mp3 file if it is time
var intervalId = window.setInterval(function () {
playazan();
console.log("Checking Prayer Time");
}, 5000);
function playazan() {
if (azan.data.timings.Asr === convertFormat) {
new Audio('https://www.islamcan.com/audio/adhan/azan20.mp3').play()
console.log("Its time for Dhuhrr");
} else {
console.log("Still time remains");
}
}