Version 3 of sleep

Updated 2002-09-11 20:36:11

This was a horribly misguided thing... dangerously appealing and guaranteed to create snake balls.

I tried to remove the code below from this page, but some evil genius put it back!


Will suspend evaluation of current code block, but allow the event loop to continue handling pending events.

This particular version of uniqkey is my favorite because the keys generated will generally sort in time created order.

 proc uniqkey { } {
     set key   [ expr { pow(2,31) + [ clock clicks ] } ]
     set key   [ string range $key end-8 end-3 ]
     set key   [ clock seconds ]$key
     return $key
 }

 proc sleep { ms } {
     set uniq [ uniqkey ]
     set ::__sleep__tmp__$uniq 0
     after $ms set ::__sleep__tmp__$uniq 1
     vwait ::__sleep__tmp__$uniq
     unset ::__sleep__tmp__$uniq
 }

Example:

 after 4000 puts foo!
 sleep 5000
 puts bar!

-Phil Ehrens