Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'this' not set correctly on first call #233

Closed
gfwilliams opened this issue Feb 12, 2014 · 0 comments
Closed

'this' not set correctly on first call #233

gfwilliams opened this issue Feb 12, 2014 · 0 comments

Comments

@gfwilliams
Copy link
Member

reset()
Pin.prototype.startFlashing = function(period) {
  if (Pin.intervals===undefined) Pin.intervals = [];
  if (Pin.intervals[this]) clearInterval(Pin.intervals[this]);
  var on = false;
  var pin = this;
  console.log(this);
  Pin.intervals[this] = setInterval(function() {
    on = !on;
    digitalWrite(pin, on);
  }, period);
};
LED1.startFlashing(1000) 
...
undefined
ERROR: Invalid pin!
ERROR: Invalid pin!
ERROR: Invalid pin!
ERROR: Invalid pin!
ERROR: Invalid pin!
...
clearInterval();
LED1.startFlashing(1000); 
// works...

However:

reset()
Pin.prototype.startFlashing = function(period) {
  console.log(this);
  var pin = this;
  if (Pin.intervals===undefined) Pin.intervals = [];
  if (Pin.intervals[pin]) clearInterval(Pin.intervals[pin]);
  var on = false; 
  Pin.intervals[pin] = setInterval(function() {
    on = !on;
    digitalWrite(pin, on);
  }, period);
};
LED1.startFlashing(1000); 
// works

It seems that just calling Pin.intervals[this]; somehow resets this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant