I am working on a app using App Lab (code.org). I am sorry if this is a very easy fix. I started javascript just 3 months ago and this is my first project.
I want the countdown to start once they click button "start_city_game_button" however I am getting this error
setInterval() callback parameter value (1000) is not a function
and
setInterval() milisecond parameter value (undefined) is not a number.
These errors appear in line 2.
var city_time = 60;
var city_time_counter = setInterval(1000);
function cityTimer() {
city_time = city_time -1;
clearInterval(city_time_counter);
}
}
I expect the timer to start once level has been started, however the time actually starts when the app starts. I have gone through many drafts of this code to find the correct anwser but I cannot find the way.
onEvent("back_mainscreen", "click", function() {
setScreen("start_screen");
});
onEvent("instructions", "click", function() {
setScreen("instructions");
});
onEvent("startgame", "click", function() {
setScreen("city_screen");
});
// City Code
var city_tries = 0;
var city_time = 60;
var city_time_counter = setInterval(1000);
onEvent("start_game_city_button", "click",function() {
city_time = city_time -1;
console.log(city_time);
setText("second_city_time", city_time);
if (city_time === 0) {
setScreen("city_screen");
clearInterval(city_time_counter);
}
});
onEvent("start_game_city_button", "click", function() {
console.log("CONSOLE: Player Started City Level");
console.log(city_tries);
setScreen("city_game_screen");
city_tries = city_tries + 1;
});
var score = 0;
onEvent("deer", "click", function() {
setPosition("deer", randomNumber(50,240), randomNumber(50, 400));
score = score + 1;
setText("score_label", score);
});
// Ocean Code
var ocean_tries = 0;
onEvent("ocean_start_button", "click", function() {
ocean_tries = ocean_tries + 1;
console.log("CONSOLE: Player Started Ocean Level. Try");
console.log(ocean_tries);
});
The first parameter to
setInterval
is a function. And second parameter is amount of time.If you want the timer to start on click then attach a listener to the element and setInterval in that.
And also if you need to decrease variable by
1
you can use a shorthand for that.The above line is same as