我对 Perl 很陌生,这是我在这里的第一篇文章,所以请保持温和.
I am quite new to Perl and this is my first post here so be gentle.
我在 60 秒的倒数计时器中使用实时,并且需要能够每 10 秒停止一次,然后用户必须输入他们是否愿意在每 10 秒的间隔继续倒计时.计时器有效,我只是不知道如何暂停它以便用户输入他们的回复.尽可能基本的答案将非常受欢迎,因为我还不太了解.谢谢,这是我目前的代码.
I am using real time in a countdown timer of 60 seconds and need to be able to stop it every 10 seconds and the user must then input whether they would like to continue the countdown or not at each interval of 10. The timer works, I just can't figure out how to pause it for the user to input their response. As basic of an answer as possible would be very welcomed as I do not understand a lot yet. Thank you and here is my code so far.
my $countdown = 1*60; #60 seconds $| = 1; #disable output buffering my $start_time = time; my $end_time = $start_time + $countdown; for (;;) { my $time = time; last if ($time >= $end_time); printf("\r%02d:%02d:%02d", ($end_time - $time) / (1*60), ($end_time - $time) / 60%60, ($end_time - $time) % 60, ); sleep(1); } 推荐答案在收到用户响应后更新 $end_time.
Update $end_time after getting the response from the user.
while (1) { $countdown--; $time = time; last if ($time >= $end_time); printf("\r%02d:%02d:%02d", ($end_time - $time) / (1*60), ($end_time - $time) / 60%60, ($end_time - $time) % 60, ); sleep(1); if ($countdown%10 == 0) { print "Continue? "; $answer = <>; chomp $answer; last if $answer ne 'y'; $end_time = time + $countdown; } }更多推荐
如何在 Perl 中实时暂停
发布评论