You are reading a single comment by @Raik and its replies. Click here to read the full conversation.
  • Hey all, I'm trying to write my first module for the pixl and I'm having difficulties with setInterval in the modules function. I was closely following the writing modules page.

    I use the code below to develop in the WebIDE. The beep function works fine, so does the second function that just calls the beep function. But then the third function involves calling beep from a setInterval. And that is not working.

    What am I doing wrong?

    var exports={};
    var PIN;
    var VOLUME;
    
    function SOUND(pin,volume){
    	this.PIN = pin;
    	this.VOLUME = volume;
    }
    
    SOUND.prototype.beep = function(freq,time){
      analogWrite(this.PIN,this.VOLUME,{freq : freq});
      setTimeout(function(){analogWrite(this.P­IN,0); },time);
      print("beep");
    };
    
    SOUND.prototype.multibeep_works_fine = function(freq,time,nr){
      this.beep(freq,time);  
    };
    
    SOUND.prototype.multibeep_works_not = function(freq,time,nr){
      setInterval(function(){ this.beep(freq,time); }, 1000);
    };
    
    exports.setup = function(pin,volume){
    	return (new SOUND(pin,volume));
    };
    
    var s = exports.setup(A0,0.005);
    
    //works
    s.beep(1000,10);
    
    //works
    s.multibeep_works_fine(1000,100,1);
    
    //does NOT work?
    s.multibeep_works_not(1000,100,1);
    
  • Sat 2020.01.25

    @Raik, is the above code in one monolithic file, as the modules file, or is there a calling page?

    Is the desire to call a separate module page, or to learn how to develop a single file to develop the module code?

    It's possible that L21 needs to also pass 'freq' 'time' as arguments - e.g. the function parameter list isn't present in order to pass the argument values.

    Haven't tested, but thinking about it, I believe the above to be true. One could have included a console.log( freq, time ) statement within the L21 function to see if the values are 'undefined' or used the debugger.



    Google to the rescue!

    google:    passing function arguments using javascript setinterval site:w3schools.com

    See example page bottom:

    https://www.w3schools.com/jsref/met_win_­setinterval.asp



    Nothing to do with module development, language syntax.

About

Avatar for Raik @Raik started