javascript - Refresh jquery object after ajax -
i have lucky spin script need random number database , let spin number.
the problem is, after document.ready, cant assign value object option again.
<input type="button" value="test" id="playbtn" onclick="getluckynumber();"/> <input type="button" id="fire" style="visibility:hidden;"/> <input type="hidden" id="luck" value=""/> function getluckynumber(){ $.ajax({ url:"ajax.php", method:"post", data:{event_id:'<?=$event_id ?>'}, }) .done(function(data){ $("#luck").val(data); $("#fire").trigger("click"); }); } $(document).ready(function(){ $('.slot').jslots({ spinner:'#fire', endnumbers:$("#luck").val(), }); });
how make "endnumbers" take new value?
update: https://jsfiddle.net/asaxpse3/1/
update: final working code
var getnewnumber=function(spinner){ $.ajax({ url:"ajax.php", method:"post", async:false, data:{event_id:'23'}, }) .done(function(data){ if(data){ spinner.options.endnumbers=json.parse(data); //console.log(data); } }); }; $(document).ready(function(){ $('.slot').jslots({ spinner:'#playbtn', onstart:getnewnumber }); });
thanks skobaljic.
you can bind function on onstart
option, have change plugin code , provide spinner instance parameter:
if ($.isfunction(base.options.onstart)) { base.options.onstart( base ); }
than should define random number function generator, example:
var getnewnumber = function( spinner ) { /* sync!! ajax stuff, when done: */ //the returned data array, eg [1,3,4] //for spinner stop @ 134 spinner.options.endnumbers = [1, 3, 4]; };
and @ end, bind generator function plugin's onstart
option:
$('.slot').jslots({ spinner: '#playbtn', endnumbers: [], onstart: getnewnumber });
please note: ajax must sync, use param async: false
.
Comments
Post a Comment