multithreading - Javascript object lifecycle -


i have javascript class below:

var myclass = function(elem, data, op) {      var options = {};      var loopforever = function() {         console.log('i cant stop me');         settimeout(function() {             loopforever();         }, 1000);     }     this.init = function() {         loopforever();     } } 

when instantiate class , call init function, loop starts , text in console every 1 second :

var ins = new myclass(); ins.init(); 

why when set instance null, thread not stop? or time create new instance , assign previous instance name, increases calls.

in production code, not have infinite loop, have business logic. need worried performance when create new instance?

when call settimeout not bound function called it. need add property object called timeoutid. long function still required being used settimeout remain in scope.

var myclass = function(elem, data, op) {      var options = {};     var timeoutid = null;      var loopforever = function() {         console.log('i cant stop me');         timeoutid = settimeout(function() {             loopforever();         }, 1000);     }     this.init = function() {         loopforever();     }     this.stop = function () {       cleartimeout(timeoutid);     } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -