Skip to content

Adds ability to cancel events #6

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ namespace timer {
setTimeout(thenDo, time)
}

/**
* Same functionality as the after block, however this returns
* an ID which can be cancelled using the cancel block.
*/
//% block="after $time do"
//% time.defl=500
//% handlerStatement=1
//% %time=timePicker ms"
export function afterID(time: number, thenDo: () => void): number {
let id: number = setTimeout(thenDo, time)
return id
}

/**
* Cancels the code scheduled to run in the after block (referenced by
* the blocks id).
*/
//% block="cancel function: $id"
export function cancel(id: number)
{
clearTimeout(id)
}

/**
* Run the attached code seperately from other code.
* This creates a seperate context for "pause" so that pauses
Expand Down