Jump to content

Variable Self updating?


incredibale3
 Share

Recommended Posts

Hello, I'm kind of experienced with javascript, html, and a few other browser scripting languages, although I am more used to 3d game programming with directx, c++, and that sort, however, it's always good to learn new things. I was wondering if javascript could possibly make it so a variable could update itself at a certain time of day by a certain percentage, or if that's not possible with javascript. To expand further on my post, would it be possible to make variable x start as 10,000 but then go up 1% each 24 hours? Or if that is not possible with javascript, which language might I use to achieve such a thing? Sorry for sounding new, but like i said, i'm more at home with 3d game programming. I'd be very grateful if you could help me.Thanks,J

Link to comment
Share on other sites

If the page is continuously open this whole time, it can be done in real time, using a function like setInterval with the Date object.If the updating can be delayed until the page is actually loaded, then a value can be stored in a cookie and then updated as needed.Or if you don't need that kind of persistence, you could write a unix timestamp right into your script to signify the starting time, get the current date/time every when the page loads, and increment your variable according to the number of elapsed days.

Link to comment
Share on other sites

Judging from what you've said, I would want a unix timestamp, but I don't know how to make one. Do you have a link to a guide or could you show my how to make one? Then again that's probably a bit much to ask, I could find out for myself.

Link to comment
Share on other sites

Hmm, I don't exactly see anything useful to make the variable automatically go up 1% every 24 hours, and I can't just make the variable changed based on the time, because i'm trying to make a 2d game with javascript where you get to be the president of a country and the variable i'm trying to set up is the population, and I need it to recognize if there's change in the variable i.e. the country being attacked and the population depleting so the growth should be less. If i'm just being dumb and there's a way in there i'm blind to poke me in the right direction please. However, I'm going to go browse the site some more and see if i can find a plausible way.

Link to comment
Share on other sites

Increasing a variable by 1%:val += (val * 0.01);You can use setTimeout to schedule something to run at a later time. The time is given in milliseconds, so if you want to schedule something to run 24 hours from now then you schedule it for 86400000 ms. Like Dad said, the HTML page must remain open in order for this to work. If someone closes or refreshes the page it will stop the code. This type of thing would probably be better done in PHP, where you read the population from the previous time it was updated, and if more than 24 hours have gone by since then then you calculate the new population, save the new population, and save the current time so you know the last time it was updated. All of that would get saved in a database.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...