• This project creates an object prototype using the setInterval function, and then creates four instances of the object with different intervals running concurrently.

    The object prototype:

    function testObj(name,mcount,interval){
      this.Name=name;
      this.Mcount=mcount;
      this.Count=0;
      this.Interval=interval;
      this.ID=0;
    
    //Methods
      this.doCount=function(){
       this.Count++;
       if(this.Count <= this.Mcount){
        console.log(this.Name," ",this.Count);
       }else{
        clearInterval(this.ID);
        console.log(this.Name," Finished");
       }//endif
      },//end doCount
    
    }//end testObj
    
    

    Two functions used to test the object:

    // Test the testObj methods 
    function Test(name,mcount, interval){
     // create an instance of testObj
    var Y=new testObj(name,mcount,interval);
    Y.ID=setInterval(function(){Y.doCount();},Y.Interval);
     console.log(Y.Name," started");
      console.log("Test End");
    }//end Test
    
    function Test1(){
     // create an instance of testObj
    var Y=new testObj("Third",10,1500);
    Y.ID=setInterval(function(){Y.doCount();},Y.Interval);
    console.log(Y.Name," started");
    console.log("Test End");
    
    var Y1=new testObj("Fourth",10,2000);
    Y1.ID=setInterval(function(){Y1.doCount();},Y1.Interval);
    console.log(Y1.Name," started");
    console.log("Test End");
    }//end Test
    
    

    The code that calls the functions:

    console.log(process.memory());
    Test("first",10,500);
    console.log(process.memory());
    Test("second",10,1000);
    console.log(process.memory());
    Test1();
    console.log(process.memory());
    //When Fourth finishes type this into the left side
    //  console.log(process.memory()); 
    //Compare the Usage at the start, as each task is added and at the end
    
    

    Some of the output in the left pane of WebIDE:

    >echo(0);
    { "free": 2144, "usage": 106, "total": 2250, "history": 101,
      "stackEndAddress": 536910652, "flash_start": 134217728, "flash_binary_end": 223300, "flash_code_start": 134449152, "flash_length": 262144 }
    first  started
    Test End
    { "free": 2076, "usage": 174, "total": 2250, "history": 108,
      "stackEndAddress": 536910652, "flash_start": 134217728, "flash_binary_end": 223300, "flash_code_start": 134449152, "flash_length": 262144 }
    second  started
    Test End
    { "free": 2013, "usage": 237, "total": 2250, "history": 112,
      "stackEndAddress": 536910652, "flash_start": 134217728, "flash_binary_end": 223300, "flash_code_start": 134449152, "flash_length": 262144 }
    Third  started
    Test End
    Fourth  started
    Test End
    { "free": 1900, "usage": 350, "total": 2250, "history": 114,
      "stackEndAddress": 536910652, "flash_start": 134217728, "flash_binary_end": 223300, "flash_code_start": 134449152, "flash_length": 262144 }
    =undefined
    first   1
    first   2
    second   1
    first   3
    Third   1
    first   4
    second   2
    Fourth   1
    first   5
    first   6
    second   3
    Third   2
    first   7
    first   8
    second   4
    Fourth   2
    first   9
    Third   3
    first   10
    second   5
    first  Finished
    
    

    1 Attachment

About