javascript, increase number day by day

2019-09-06 03:29发布

I need to write a code with javascript which increase the given number and sum it with a random range numbers and finally displays it. This has to be saved, because the next day, this should increment current result, using the same "sum with random range of numbers".

for instance:

Given number: 135

Random range: 5 to 20

So:

Current day=135+random range, then keep it for next 24 hours.

Next day=Current day+random range, then keep it for next 24 hours....

but I don't know how to do it, would you please help me? I really appreciate it.

3条回答
家丑人穷心不美
2楼-- · 2019-09-06 04:04

An function to get a number like this is:

function RandVal(minVal, maxVal)
{
    var randVal = Math.round(minVal+(Math.random()*(maxVal-minVal)));
}

So if you want to use local storage(cookies) you add to function:

document.cookie = "randVal=" + randVal;

For further information: W3Schools JS Cookies

But if you want to store on an database in a wab server, you have to use ajax:

W3Schools Ajax Tutorial

But it don't makes sense to use JavaScript to do this if you'll use a server-side application (e.g. PHP, JSP, ASP etc...) indeed. It will make more sense to do everything on server-side, in my oppinion.

查看更多
3楼-- · 2019-09-06 04:11

Also, you could use your own simple pseudo-random generator, which would calculate current day value depending on the initial numbers (maybe stored in cookies) and a current date. It could save space, because cookies are very strict in size.

查看更多
欢心
4楼-- · 2019-09-06 04:17

If your aim is a client-side application and you don't have to support old browser I would suggest to use local-storage. With local-storage you can save your value. The next day you open the browser with the JavaScript-App and it will update your number. Instead starting the app manually you can use e.g. the timeout function.

查看更多
登录 后发表回答