times <- function(total = 3, str = "Task a") {
ctr <- 0
function(expr, value, ok, visible) {
ctr <<- ctr + 1
cat(str, ctr, "\n")
keep.me <- (ctr < total)
if (!keep.me)
cat("handler removing itself\n")
# return
keep.me
}
}
# add the callback that will work for
# 4 top-level tasks and then remove itself.
n <- addTaskCallback(times(4))
# now remove it, assuming it is still first in the list.
removeTaskCallback(n)
## See how the handler is called every time till "self destruction":
addTaskCallback(times(4)) # counts as once already
sum(1:10) ; mean(1:3) # two more
sinpi(1) # 4th - and "done"
cospi(1)
tanpi(1)
Run the code above in your browser using DataLab