javascript - jQuery function callback does not work in custom class/object -


so newbie programming excuse me if novice question. creating simple wave effect create illusion of moving water. did simple function in jquery , worked fine. learning oop code more organized project working on. created class handle animation. animation works fires once. need keep firing create illusion. function callback works normal procedual way not seem work within object class. have researched , researched , can not find answer specific problem. appreciated. code below.

    function waves(selector){       this.selector = selector;       this.animatewaves = function(forward,backward,speed){         $(selector).velocity({translatex: forward},speed);          $(selector).velocity({translatex: backward},speed,animatewaves);       };     };    var surfacewaves = new waves('#topwaves');    surfacewaves.animatewaves('+=40','-=40','1000'); 

edit actually, first solution not work because context of callback element, not object. here working code , jsfiddle:

function waves(selector){   this.selector = selector;   this.animatewaves = function(forward,backward,speed){     var self = this;      $(selector).velocity({translatex: forward},speed);        $(selector).velocity({translatex: backward}, speed, function () {           self.animatewaves(forward,backward,speed);       });   }; }; var surfacewaves = new waves('#topwaves'); surfacewaves.animatewaves('+=40','-=40','1000'); 

removed old code incorrect code


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -