I'm playing around with making a little game in JS.
There's a day counter that increments every seconds through setInterval().
I would like to access the incremented variable daycount from inside the main new_game() function to trigger game events based on days that have passed.
Would appreciate if someone could help or point me in the right direction.
var day = $('#time');
var daycount = 0;
$(document).ready(function () {
$('#start').click(function () {
new_game();
$(this).addClass('disabled').html('Started');
})
});
function time() {
setInterval(function () {
daycount++;
day.html('Day ' + daycount);
return daycount;
}, 1000);
}
function new_game() {
time();
if(daycount == 5){
alert('something');
}
}
day.html('Day ' + count);should beday.html('Day ' + daycount);to start with. Secondly, you will only see if alert dialog if you click the start button when the daycount is exactly 5