Created
December 4, 2022 08:02
-
-
Save kush-agra/a80df0b554dad614ab24bb3b9c9b50ef to your computer and use it in GitHub Desktop.
function that allows you to specify the time at which you want to make the API call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeApiCallAtSpecifiedTime(hour, minute) { | |
var date = new Date(); | |
var currentHour = date.getHours(); | |
var currentMinutes = date.getMinutes(); | |
// check if it is currently past the specified time | |
if (currentHour > hour || (currentHour === hour && currentMinutes > minute)) { | |
// if it is past the specified time, call the API immediately | |
callApi(); | |
} else { | |
// otherwise, calculate the time until the specified time and set a timeout to call the API at that time | |
var timeUntilSpecifiedTime = (hour - currentHour) * 60 - currentMinutes + minute; | |
setTimeout(function() { | |
callApi(); | |
}, timeUntilSpecifiedTime * 60 * 1000); | |
} | |
} | |
//example | |
makeApiCallAtSpecifiedTime(6, 0); // calls the API at 6am | |
makeApiCallAtSpecifiedTime(12, 30); // calls the API at 12:30pm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment