i have this script for a Whack a mole game, but i can't seem to make my div's pop up when I want them to by using a random number generator. This is my code:(by the way this is my first game I have ever made so it's pobably a stupid question :))
//minigame
setInterval(function() {
var randomnumber = 1 + Math.floor(Math.random() * 10);
if (randomnumber = "1") {
$('#mole1').show();
};
if (randomnumber = '2') {
$('#mole1').show();
};
if (randomnumber = '3') {
$('#mole1').show().delay(300).hide();
};
if (randomnumber = '4') {
$('#mole1').show().delay(300).hide();
};
if (randomnumber = '5') {
$('#mole1').show().delay(300).hide();
};
if (randomnumber = '6') {
$('#mole1').show().delay(300).hide();
};
}, 200);
and my html that's relevant:
<div id="minigameblockholder">
<div class='moles' id="mole1"> </div>
<div class='moles' id="mole2"> </div>
<div class='moles' id="mole3"> </div>
<div class='moles' id="mole4"> </div>
<div class='moles' id="mole5"> </div>
<div class='moles' id="mole6"> </div>
<div id="scorebord"> </div>
</div>
I have not started working on my scoreboard yet so don't other correcting that. thanks in advnce, Jasper
setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .
I want to return a value inside a setInterval. I just want to execute something with time interval and here's what I've tried: function git(limit) { var i = 0; var git = setInterval(function () { console. log(i); if (i === limit - 1) { clearInterval(git); return 'done'; } i++; }, 800); } var x = git(5); console.
Stopping the Function You'll need to use the clearInterval() method. It's meant to stop the timer set by using the setInterval JavaScript function. The setInterval() returns a variable called an interval ID.
The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...)
I don't know if you edited your answer, however I can see some things that would prevent it from working.
Your if statements will not work. You are using an assignment operator ("="), this is how you assign variables.
var x = 2;
(assign 2 to the variable x)
You need to use "==" (is equal to) operator in comparisons.
if(randomnumber == 2)
(if randomnumber is equal to 2)
Following on from my comment, I can't see from your codefragment why it isn't working. However, you could try something like the following;
setInterval(function(){
var randomnumber = 1 + Math.floor(Math.random() * 10);
$('#mole1').html(randomnumber);
var mole = $('#mole' + randomnumber);
if(mole != undefined){
$('.moles').hide();
mole.show();
}
}, 2000)
This creates a random number, attaches it to the id "mole" (eg: mole + 2) and checks to see if it is defined (exists). If it is, it hides all the other moles and shows the selected mole (in our example, 2). This will also get rid of all of the if statements and will allow you to increase or decrease the number of moles without having to create extra checks for them.
You can see it working here https://jsfiddle.net/ezs00xw0/
Note: Ignore the extra html and css, this was for debugging purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With