如何在 Perl 中实时暂停

编程入门 行业动态 更新时间:2024-10-24 13:21:27
本文介绍了如何在 Perl 中实时暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对 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 中实时暂停

本文发布于:2023-11-27 03:03:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实时   如何在   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!