JavaScript Creating a timer for a javascript game gives undesirable results create timer javascript,creating timer js,create countdown timer javascript,

I am designing a game in HTML5 with Canvas/JavaScript. My aim is to create a timer that will countdown from a certain amount of minutes down to zero, during the time you need to shoot as many enemies as you can. I have been busy searching for effective timers but some implementations caused issues which crashed the browser (probably infinite loop) or the countdown of (timeInSeconds ) goes down way to fast.

this is the code i most recently implemented to try to get it to work

function setTimerCountdown(){
timeInSeconds = timeInSeconds - 1;
    ctx.fillText(timeInSeconds, 200,180);
}

I have already got a setInterval which is used to update the game here is the code for that, this code lies at the bottom of the js file

var main = function () {

var now = Date.now();
var delta = now - then;
update(delta / 1000);
render();
then = now;
};

// Let's play this game!
reset();
var then = Date.now();
var fps = 60;
setInterval(main, 1000/fps); // Execute as fast as possible

i then have additional code such as the render function, which renders the pictures to the screen and the text, here is the code for this

var render = function () {

returnKillsNeeded(stageNum);
            ctx.drawImage(startGameImg, 0,0);
            ctx.font = "24px Helvetica";
            ctx.textAlign = 'center';
            ctx.textBaseline = "top";
            ctx.fillText("press Enter to start", 250, 300);
            ctx.fill();
    if(gameStart){
if (bgReady) {
ctx.drawImage(bgImage, 0, 0);


}
setInterval(setTimerCountdown, 1000);
setTimeOut(setTimerCountdown, 1000);
ctx.fillStyle="#522900";
ctx.fillRect(0,480,500,120);

the idea is to countdown the time from 120 ( i do know that there is no catch available to stop time going in to negative values, as well as unnecessary code namely currentTime and time. As mentioned several attempts of implementation had proven unsuccessful,

My questions are, should i have the setInterval or setTimeOut or any aspect of a timer in the render method?, is the code mentioned quoted above the same code i should use to create the timer?. I know that there is quite a few examples out there but i can't seem to break into how i can apply that code into mine without screwing up all the work i've done so far. I had links attached i was willing to use to show you my research but due to frustrating (fix your code structure) popups on this site i had excluded links to a video from a youtube channel called goTreeHouse - create a timer in javascript and several stack questions already asked.

To add further possibly relavant info. I am not using any API's (JQuery etc). just HTML5, CSS, JavaScript (using notepad++).

Assistance is appreciated, also point out any faults in the code, that you currently see. Also i suppose another thing div tags, only 1 being used is a div tag for the canvas, all the text, images and music is through js, once again without additional API's

edit: below is the code with the edit, also note the if statement block in the render function, this is to stop the minus figures from showing up on screen (it does not however take into account that in the background the function is still updating and the negative value is getting bigger, i will look into addressing this problem.

//Thanks to GameAlchemist
function createCountDown(timeRemaining) {
    var startTime = Date.now();
    return function() {
    return timeRemaining - ( Date.now() - startTime );
    }
}
var currentCountDown = createCountDown(30000);

// Draw everything
var render = function () {



var countDownValue = currentCountDown();

returnKillsNeeded(stageNum);
            ctx.drawImage(startGameImg, 0,0);
            ctx.font = "24px Helvetica";
            ctx.textAlign = 'center';
            ctx.textBaseline = "top";
            ctx.fillStyle="#FF0000";
            ctx.fillText("press Enter to play", 250, 450);
            ctx.fill();
    if(gameStart){
if (bgReady) {
ctx.drawImage(bgImage, 0, 0);


}

ctx.fillStyle="#522900";
ctx.fillRect(0,480,500,120);
ctx.drawImage(scoreImg, 22,522);
ctx.drawImage(livesImg, 360,522);

ctx.drawImage(progressImg, 200,492);
createProgressBar();
createProgressPercent();
ctx.fillText("progress", 170,492);
setEnemyHealthText();
drawPlayer();
if(countDownValue <=0){
    countDownValue = 0;
    }else{
ctx.fillText(countDownValue, 200,190);
}
Answer:1

Hello guys I have some problem with my website. Here's the situation: Page 1 <script type="text/javascript" src="jscript.js"> <input type="button" value="change" onClick="changeAttr()"> ...

Hello guys I have some problem with my website. Here's the situation: Page 1 <script type="text/javascript" src="jscript.js"> <input type="button" value="change" onClick="changeAttr()"> ...

  1. change element attribute javascript
  2. change element attribute jquery
  3. change element attribute microstation
  4. change element attribute selenium
  5. change element attribute value jquery
  6. change element attribute
  7. change element attribute css
  8. change element attribute angular
  9. change element attribute angularjs
  10. change element attribute selenium python
  11. change element attribute html
  12. js change element attribute
  13. change html element attribute javascript
  14. vue change element attribute
  15. react change element attribute
  16. js change element attribute value
  17. puppeteer change element attribute
  18. vuejs change element attribute
  19. protractor change element attribute
  20. change dom element attribute

In our frontend build scheme we need to resize images for srcset attribute. I've tried to use gulp-responsive and other Gulp and Grunt plugins, but quality of result images is unacceptable. So, can I ...

In our frontend build scheme we need to resize images for srcset attribute. I've tried to use gulp-responsive and other Gulp and Grunt plugins, but quality of result images is unacceptable. So, can I ...

I'm trying to get WebPack set up with TypeScript and some external libraries, but having some trouble. I'm importing the src version of jQuery in order to let WebPack choose the required modules (...

I'm trying to get WebPack set up with TypeScript and some external libraries, but having some trouble. I'm importing the src version of jQuery in order to let WebPack choose the required modules (...

I've got a more complex structure which breaks down into something like this: http://jsfiddle.net/HGLNz/14/ <ul> <li class="item top"></li> <li class="item top-middle"&...

I've got a more complex structure which breaks down into something like this: http://jsfiddle.net/HGLNz/14/ <ul> <li class="item top"></li> <li class="item top-middle"&...