Reputation: 1
I am trying to make a slotmachine. I am a beginner in Javascript.
I made three slots and then wanted to spin the slot. I used 'setInterval' to spin it.
function drawSlot1(){
var slot1Value = document.getElementById('slotImage1');
var r1 = Math.floor(Math.random() * 9);
slot1Value.src = "graphics/" + fruitArray[r1] + ".jpg";
}
setInterval(drawSlot1, 100);
setTimeout(slotTimeout1, 2000);
and tried with console.log to see the value at slot1Value.
It gave me all values generated during the interval. similarly, the other two slots are also made in the same manner. now i want it to give only the last value (value after spinning stops) in all three slots so that i could compare the values generated and use it for scoring purpose as well as activating NUDGE and HOLD buttons. How do i do it? Any help is most appreciated.
Upvotes: 0
Views: 2617
Reputation: 1036
If i understood you your problem description right the solution would be to store r1 value (from drawSlotX function) to global, or wider scoped (than drawSlot function) variable.
var solt1NumericValue;
function drawSlot1()
{
var slot1Value = document.getElementById('slotImage1');
var solt1NumericValue = Math.floor(Math.random() * 9);
slot1Value.src = "graphics/" + fruitArray[solt1NumericValue] + ".jpg";
}
When you reach spinning end you can check the value of solt1NumericValue. It should store value that was set during last interval call.
And a sample for animating the spin: https://jsfiddle.net/spwwtzcd/1/
Upvotes: 1