2
I wanted to know how to do a single execution, example:
In my application there is a way to schedule charges and they are scheduled per day, then on a certain day X she will be charged.
I’m currently doing it this way:
const da = Number(new Intl.DateTimeFormat('pt-br', { day: '2-digit' }).format(new Date(Date.now())));
let arr = [
{ name: "Salário", amount: 150000, day: 5 },
{ name: "Internet", amount: -11590, day: 10 },
{ name: "Teste", amount: 15000, day: 5 },
];
const indexofday = arr.reduce((acc, obj, index) => {
if (obj.day === da) {
acc.push(index);
}
return acc;
}, []);
const newarr = indexofday.map((item) => Array[item]);
//Função para adicionar essa array em outro local da aplicação. O concat e somente um exemplo!
newarr.forEach((transaction, index) => {
datastorage.concat(transaction, index);
});
This function is what I am currently using, but when I refresh the page and the day remains the same, it runs this function again! It creates a loop in my application. Is there any way to perform this only once a day?
I don’t know any other way than not to use some browser storage and persist some data to be checked before running the function, because when reloading the page, all JS will run again. I’ll wait to see someone propose some creative solution.... or not.... or say I’m wrong :).
– Cmte Cardeal
Remember that you do not need to obtain the indexes and then access, through indexing, the respective elements. You can do this in a single "reduction".
– Luiz Felipe
That’s interesting, good to know thank you so much! I’ll do it straight next time!
– Ellathet
@Ellathet, vc means once a day (regardless of the time, each 24-hour cycle) for something that has a monthly cycle (each one x day of the month)...what comes to mind would be a cookie. When executing the function checks if there is a cookie, if it does not exist (first time) create one. You can use timestamp as value and 30 days as expiration, if it exists compares its value (timestamp)...if it has passed 24 hours runs the function again and saves a new one cookie with the current timestap, if not use
return
to leave the function without doing anything. Every 30 days the cookie is excluded and a new created.– Lauro Moraes
@Lauromoraes, I understood, I didn’t understand very well without some example, I never used this function of cookies. I’ll do some research, if you can help me by sending me an example of
timestamp
, thanks!– Ellathet
@Ellathet, published an example as a response. try to suit your needs
– Lauro Moraes