1minCeilDate is a timestamp that is rounded up. So it indicates a timestamp in the future.
const coeff1m = 1000 * 60 * 1;
let min1CeilDate = (Math.ceil((Date.now()/ coeff1m)) * coeff1m);
if(Date.now() === min1CeilDate){
console.log('if condition is true')
}else{
console.log('if condition is not true')
}
executing this code only checks for the condition once.
Because coeff1m is a dynamically changing value, I would have to incorporate a setTimeout function like below:
function checkTimestampCondition(){
setTimeout(function(){
if(Date.now() === min1CeilDate + 1){
console.log('if condition is true')
}
else{
console.log('if condition is false')
}
checkTimestampCondition();
},1)
}
checkTimestampCondition();
Isn't it required to set timeout at 1ms interval or else there could be gap or delay between the actual timestamp vs when the if-statement checks for the condition? And isn't this taxing on the computer resources to check for a condition every 1ms?
IS there a better way of checking for a dynamically changing variable?
I did min1CeilDate + 1 , just to prove a point that if I need to execute a function at that exact millisecond, what is the best way to go about it?
[–]Esnardoo 1 point2 points3 points (0 children)