How to build a setInterval-like task?

This is my idea

task=@task begin
while true
sleep(1)
foo()
if should_end
    # ?
end
end
end

But

  1. are there any simple ways to contact other than using global should_end?
  2. how to end the task from inside the expression?

stackoverflow sync

P.S.
The problem is solved in an answer in stackoverflow.

You mean repeatedly calling a function until some condition is met? You may want to look into Timer.