closures - Javascript setTimout within an object/function help -


I am new to shutting down and understanding 'javascript' to get me the most work from 'me' and i I am thinking of how I can improve the following which was trying to try the object in which there was a counter in it ... trying to improve my understanding.

EDIT: The following code works, but ... is the possibility that this is wrong (is it?) ... I have no idea about whether it is right or wrong. .. Where can I improve ... the better way is the timer in any object / function?

  function myObj () {this.outputId = 'div_output1'; This.counter = 0; This.limit = 10; This.count = function () {// its own reference I = this; If (me.doStuff (me)) setTimeout (function () {me.count ();}, 1000); }; This.doStuff = function (me) {if (me.counter> = me.limit) {document.getElementById (me.outputId). WarryText = 'reached the count limit'; return false; } And {document.getElementById (me.outputId) .innerText = 'count =' + me.counter; Me.counter + = 1; Back true; }}}  

// Example of the use of the object ...

  window.onload = function () {var x = new myObj; X.outputId = 'div_output2'; X.count (); Var y = new myObj; Y.limit = 8; Y.count (); }  

You are shutting down correctly because when setTimeout calls your function , Then this will be a 'window' object and you have to close one (which you did by specifying me this') and to access it

However, I still have a slightly different I will write my code in the manner I call it doStuff automatically instead of making it a true / false return and then decide whether to call DustFoot again.

I do not like that you do not need this 'object' for this to understand that 'how' this works in javascript, check it out.

Function Counter (Ops) {this.outputId = opts.outputId || 'Div_output1'; This._currentCount = 0; This.limit = opts.limit || 10; This.count = function () {this.deferDoStuff (); }; This.deferDoStuff = function () {var me = this; SetTimeout (function () {me.doStuff ();}, 1000); }; This.doStuff = function () {if (this.ccurrentCount> this.limit) {document.getElementById (this.outputId). WinnerHTML = 'Access to calculation limit'; Return; } Document.getElementById (this.outputId). WinnerHTML = 'count =' + this._currentCount; This._currentCount ++; This.deferDoStuff (); }; }

Usage:

  var x = new counter ({'outputId': 'div1'}); Var y = new counter ({'outputId': 'div2'}); X.count (); Y.count ();  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -