Skip to content

Instantly share code, notes, and snippets.

@betzrhodes
Created February 16, 2016 00:19
Show Gist options
  • Save betzrhodes/154d4031a53b5ed76e6e to your computer and use it in GitHub Desktop.
Save betzrhodes/154d4031a53b5ed76e6e to your computer and use it in GitHub Desktop.
Electric Imp imp.wakeup/bindenv example
local global_this = this;
class Test {
var1 = null;
var2 = null;
timer = null;
test_this = null;
constructor(one, two) {
var1 = one;
var2 = two;
test_this = this;
}
function printOne() {
server.log(var1);
}
function printTwo() {
server.log(var2);
}
function startTimer() {
server.log("startTimer this is equal to test_this:");
server.log(this == test_this);
printOne();
printTwo();
timer = imp.wakeup(5, startTimer.bindenv(this));
}
function stopTimer() {
server.log("stopping timer")
imp.cancelwakeup(timer);
}
function badTimer() {
server.log("badTimer this is equal to test_this:");
server.log(this == test_this)
local bad_timer = imp.wakeup(5, function() {
server.log("in badTimer wakeup this is equal to global_this: ");
server.log(this == global_this);
});
imp.wakeup(10, function() {
server.log("stopping bad timer");
imp.cancelwakeup(bad_timer);
})
}
}
// Initialize and use Test class
t <- Test(1, 2);
t.printOne;
t.printTwo;
t.startTimer();
imp.wakeup(20, t.stopTimer.bindenv(t));
imp.wakeup(25, function() {
t.badTimer();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment