javascript - Restrict amount of times jQuery slider fires change event? -


i have problem jquery mobile slider fires handled on server. have this:

$("#testslider").change(function( event, ui ) {         $.getjson($script_root + '/_update_sliders', {         c: $('#testslider').val()   }, function(data) {     g4.updateoptions( { 'file': data.result } );         }); 

this works fine, /_update_sliders starts function in python sends data.result site. problem occurs if change slider fast - many requests send, when stop slider takes quite time catch , mixes requests - end state might not present actual slider value.

what clean solution this? anyway restrict amount of times change event fires up?

thank , kind regards

lakerz

you put throttle on using following concept. uses settimeout() add delay, , if changes happening delay gets pushed , not fire until full delay period has ended

var slidertimer,     sliderajaxdelay = 100;  $("#testslider").change(function (event, ui) {     if (slidertimer) {         cleartimout(slidertimer); /* if timeout, clear */     }      // throttle requests using settimeout      slidertimer = settimeout(sliderajaxupdate, sliderajaxdelay); });      function sliderajaxupdate() {     $.getjson($script_root + '/_update_sliders', {         c: $('#testslider').val()     }, function (data) {         g4.updateoptions({             'file': data.result         });     }); } 

adjust delay variable suits you


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -