Skip to content

Update randomizer by AJAX #2

@Howitzer

Description

@Howitzer

I needed a way to set the randomizer return value after each spin via AJAX (I'm using PHP to prevent client side injection). I was able to add a new function that accepts the index of the chosen element... maybe there's a better way but here's a snippet of my solution.

/**
  * @desc PRIVATE - Updates the index to the new custom value
*/
function _updateCustom(indexToSet){
    _active.index = indexToSet;
}


/**
 * @desc PUBLIC - SET chosen element
*/
setCustom : function(indexToSet){
    if(typeof indexToSet==='number' && indexToSet>=0 && indexToSet<$titles.length){
        _updateCustom(indexToSet);
    }
}

Then I did something really bad with the _getCustom() private function:
I changed var index = self.settings.randomize(_active.index); to just var index = _active.index;

And then my usage was simply:

$("#pull").click(function() {
  $.ajax({
    url: 'ajax/slotmachine_ajax_responder.php',
    type: 'POST',
    data: {'method': "spin"},
    dataType: 'json',
    cache: false,
    success: function(data) {
        if (data.success) {
            //Set finishing placements
            machine1.setCustom(data.num1);
            machine2.setCustom(data.num2);
            machine3.setCustom(data.num3);

            //Do the spinning
            machine1.shuffle(3, onComplete);

            setTimeout(function() {
                machine2.shuffle(3, onComplete);
            }, 250);

            setTimeout(function() {
                machine3.shuffle(3, onComplete);
            }, 500);
        }
    }
  });
});

Please let me know where I can improve on this. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions