Loop a function evry x amount of time

Hi,

Got a small questiong
Does anyone know if there is a way to repead a function evry X amount of time?

Don’t know if you know cookie clicker,
it goes as following;
You press a cookie you get a cookie,
You buy stuff so evry click gives more cookies,
after a bit you can upgrade so you automaticly get cookies.
(so a function will be in a loop “generating” your cookies/sec)
you can upgrade more so evry upgrade you get more and more cookies/sec.

I’m planning on making a game that has some things that do the same.
but i only know the settimeout function and i don’t know if there is another way of doing this.

Then the probably even harder part…
Is there a way to keep doing it if the member isn’t logged in anymore
something that would keep doing it in the backend?

Kind regards,
Kristof

If you’re talking about FRONT-END code then you should call the setInterval() function:
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

Thanks i will take a look at it later.

Any idea if there is something that would be able to run constantly in the back-end?
that would keep running even when the users are offline?

Seems like its working like a charm :smiley:

If there is a way to keep doing this for each user when they are offline,
Please let me know :slight_smile:

@volkaertskristof to do a recurring job on the backend, you can use Job Scheduler . However in that case you can’t run it more than one time per hour.
(I don’t know what exactly you’re trying to do, so I can’t tell if it fits to your requirements).

Hi @jonatandor35 ,

Its been a while since i asked this and now i finaly found the time to start my project.

I don’t know if you remember the time where Criminal/mafia text-based games where a thing?
They where writen in html php and db was mysql.
depending on buildings you had you get x amount of money/hour.
But you also had games gave money evry minute.

I am trying to make such thing with wix.
Don’t know if its gona work but i think it will (if there aren’t to many users at the same time).

Anyway the interval can be used in the front end for some things maybe,

The job scheduler is very helpfull for my 1 hour money thingy :sweat_smile:

When i am done i’ll link it here :stuck_out_tongue:

Kind regards,
Kristof