简体   繁体   中英

Countdown from 60 to 0 in javascript and loop

As some of you guys may know, I am still developing my game LOL.

Right now I have basic functionality. Players can see their profits display and buy stuff if they have enough money.

All i need now is to display how quick players are getting money. So players are supposed to have money generated every 60 seconds. The money gets generated but it has no visuals.

What i want is for people to see how much to they have left before they get the next money. 在此处输入图片说明

so i have all the styling and that done for the timer. What i want is some vanilla javascript code to display the innerHTML for the time decrementing from 60 down to 0;

I have tried intervals with if statements and switch cases and ive tried a for loop but i cant get it to reset or even do anything.

Below is some of my code.

Code:

 // Income Ticker Display (displaying time until next pay day) var incomeTicker = 60; window.setInterval(function(){ if (incomeTicker > 0) incomeTicker--; document.getElementById("incomeTicker").innerHTML = "Next Profit In : " + incomeTicker + " seconds"; }, 1000); 
 <span class = "incomeTicker" id = "incomeTicker" > Next Profit In : 60 seconds </span> 

i have tried different ways of doing this and have looked a few questions but nothing is giving me what i need.

Thanks for any advise in advance.

ps. let me know if you need more code examples.

OKAY I FIXED MY LOOPING ISSUE AS WELL

 // Income Ticker Display (displaying time until next pay day) var incomeTicker = 60; window.setInterval(function(){ if (incomeTicker > 0) incomeTicker--; document.getElementById("incomeTicker").innerHTML = "Next Profit In : " + incomeTicker + " seconds"; if (incomeTicker <= 0) incomeTicker = 60; }, 1000); 
 <span class = "incomeTicker" id = "incomeTicker" > Next Profit In : 60 seconds </span> 

Thank you every one for helping me solve this problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM