取消后不在tcl中工作
我已经编写了以下代码以在取消后进行测试。这个代码应该只打印一次“现在更新”,但它打印了10次,所以有人能告诉我为什么取消后不工作吗取消后不在tcl中工作,tcl,Tcl,我已经编写了以下代码以在取消后进行测试。这个代码应该只打印一次“现在更新”,但它打印了10次,所以有人能告诉我为什么取消后不工作吗 proc update_now {} { puts "Now updating" } proc print_now {} { after cancel [update_now] after idle [update_now] } for {set count 0} {$count < 5} {incr count} { prin
proc update_now {} {
puts "Now updating"
}
proc print_now {} {
after cancel [update_now]
after idle [update_now]
}
for {set count 0} {$count < 5} {incr count} {
print_now
}
proc update_now{}{
将“正在更新”
}
proc print_now{}{
取消后[立即更新]
空闲后[立即更新\u]
}
对于{set count 0}{$count<5}{incr count}{
立即打印
}
你很困惑。首先,在调用update\u now
的结果上(实际上是一个空字符串,它是一个不可操作的脚本),而不是在脚本上调用update\u now
,在空闲后使用和取消后使用。Tcl对引用和使用的要求非常严格。相反,您想要的是更像:
proc print_now {} {
after cancel update_now
after idle update_now
# You could use {update_now} or [list update_now] too; no real difference here
}
第二,您应该真正关注仅通过令牌而不是根据您搜索的内容取消。要做到这一点,您实际上要做到:
proc print_now {} {
global print_now_token
after cancel $print_now_token
set print_now_token [after idle update_now]
}
# Initialise the variable
set print_now_token {}
取消后不接受脚本,它接受空闲后返回的句柄。