Counting click with trigger('click') and setTimeout (have fiddle)

Hi,

So I have this fiddle: http://jsfiddle.net/bb0Lschd/4/

And what it is is a slot machine using jSlots jquery plugin. However, I want the machine to trigger on load, and did so with trigger(‘click’) on window load, and then I did a setTimeout on the function so it repeats itself with 1000ms paus.

However, the “repeats” doesnt seem to count as clicks, because I get the same ending number for each “slot-roll”. And as you can see that is not what I want, so i set different ending numbers for each click.

Anyone know how this can be fixed?

I am a beginner at jquery/js , and maybe there is some better way to go through three different ending number -scenarios, without counting clicks? Like doing an array in an array or something? array(array(‘1’,‘1’,‘1’), (array(‘2’,‘2’,‘2’),(array(‘3’,‘3’,‘3’));

Going from a working example using multiple sets of end numbers, I’ve made several changes so that the button can be replaced with a click event on the spinner itself.

The array of results can be done as follows:

var slotNumbers = [
    [1, 4, 3],
    [1, 4, 6],
    [1, 4, 1]
];

You can use the modulus operator to pick out one of those slotNumbers, with clickCount % slotNumbers.length

function setSpinNumber() {
    jsSlot.options.endNumbers = slotNumbers[clickCount % slotNumbers.length];
    clickCount += 1;
}

jsSlot comes from when we set up jSlots, where we assign the .first slot element to jsSlot. We can also assign the above setSpinNumber to the onStart event.

jsSlot = $('.slot').jSlots({
    spinner: '.slot',
    onStart: setSpinNumber
})[0];

Then you’re wanting to spin things every eight seconds:

setInterval(function () {
    $('.slot').click();
}, 8000);

And lastly, you can spin it when the page starts too, by triggering the click event as a part of setting things up.

$(document).ready(function () {
    ...
    $('.slot').click();
});

All put together, we end up with the following code:

$(document).ready(function () {
var clickCount = 0,
jsSlot;

    function setSpinNumber() {
        var slotNumbers = [
            [1, 4, 3],
            [1, 4, 6],
            [1, 4, 1]
        ];
        jsSlot.options.endNumbers = slotNumbers[clickCount % slotNumbers.length];
        clickCount += 1;
    }

    jsSlot = $('.slot').jSlots({
        spinner: '.slot',
        onStart: setSpinNumber
    })[0];

    $('.slot').click();
    setInterval(function () {
        $('.slot').click();
    }, 8000);
});

And the following jsfiddle has a demo of it in action:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.