I would like to show on my website some data, which should be changing every 30 seconds.
However, when someone open my website and there is 18 seconds left to change, JS should wait 18 seconds, change the data and then set setInterval().
I tried this code:
var howmany = 1502; //seconds
var rest = howmany % 30开发者_JAVA百科
if(rest > 1) {
window.setTimeout(function() {
upgradeProducts();
setInterval("upgradeProducts()", 15000);
}, rest*1000);
}
else {
setInterval("upgradeProducts()", 15000);
}
but it is not working. Where did I make mistake?
You need to take reference to current time, which would be a cross-reference between every user of your website.
In your case, the starting point is the same for everyone (30 % of 1500 seconds).
精彩评论