javascript - stop a previous function from continuing to be excuted with jquery -


i have following function create slider. works (almost perfectly)... problem i'm having once click down on slider moves around should, can't figure out how stop moving when release mouse?

suggestions? thanks!

var moveslider = function(){      //sets current position , offset variables     var currentposition;     var offset;      //looks mousedown event on slider     $("#slider").mousedown(function(e){         //sets offset = mouse coordinate of page - offset of slider         offset = e.pagex - this.offsetleft;         console.log("offset: " + offset);          //tracks mosemovement in document         $(document).mousemove(function(e){             currentposition = e.pagex - offset;              //takes mouse current position (minues offset) , sets absolute position             $('#slider').css('left', currentposition + "px");             console.log("current position: " + currentposition);              //checks make sure it's within box             if(currentposition <= 0){                 $('#slider').css('left', 0 + "px");             }else if(currentposition >= 400){                 $('#slider').css('left', 400-20 + "px");                 }         });     });      $("#slider").mouseup(function(){         $('#slider').css('left', currentposition + "px")             console.log("fixed");      });  }; 

edit: mvchr, went off example, , came following. mouse move still works, when release mouse keeps moving. im sure i"m missing stupid

edit, again: silly mistake, still had mouse move in there. took out , works now! :)

thanks again

var moveslider = function(){      //sets current position , offset variables     var currentposition;     var offset;     var rightoffset      //looks mousedown event on slider     $("#slider").mousedown(function(e){         //sets offset = mouse coordinate of page - offset of slider         offset = e.pagex - this.offsetleft;         console.log("offset: " + offset);         $(document).bind('mousemove', mmhandler);     });       var mmhandler = function (e) {              //tracks mosemovement in document             $(document).mousemove(function(e){                 currentposition = e.pagex - offset;                  //takes mouse current position (minues offset) , sets absolute position                 $('#slider').css('left', currentposition + "px");                 console.log("current position: " + currentposition);                  //checks make sure it's within box                 if(currentposition <= 0){                     $('#slider').css('left', 0 + "px");                 }else if(currentposition >= 400){                     $('#slider').css('left', 400-20 + "px");                     }             });         };       $(document).mouseup(function(e) {       // code       $(document).unbind('mousemove', mmhandler);     });   }; 

in mouse event handler add in:

$(document).unbind('mousemove');

you should put function handling bound mouse move can pass unbind because code above affect mousemove handlers on document may set.

unbind api docs


Comments