How to pause real time in Perl

前端 未结 2 1322
旧时难觅i
旧时难觅i 2021-01-16 22:08

I am quite new to Perl and this is my first post here so be gentle.

I am using real time in a countdown timer of 60 seconds and need to be able to stop it every 10 s

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 22:48

    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;
        }
    }
    

提交回复
热议问题