What's going on, please, and why?

I'll have a crack at answering this. Its easier to understand when you simplify it down to psuedo-code:

while set $time to current time test $time < $expiry fail: break loop test $time - $prevtime >= 10 fail: sleep 10 $prevtime = $time warn $time
So, this is what is happening:
$expiry = 3600 $prevtime = undef $time = undef 1st loop: $time = 123 $time 123 < $expiry 3600 (true) $time 123 - $prevtime undef >= 10 (true) $prevtime = 123 warn $time 123 2nd loop: $time = 123 (first iteration takes less than 1 second) $time 123 < $expiry 3600 (true) $time 123 - $prevtime 123 >= 10 (false) sleep 10 $prevtime = 123 warn $time 123 3rd loop: $time = 133 (slept on last iteration so advance 10s) $time 133 < $expiry 3600 (true) $time 133 - $prevtime 123 >= 10 (true) $prevtime = 133 warn $time 133 4th loop: $time = 133 (last iteration took less than 1s) ...
You can clearly see the pattern of sleep, no sleep, sleep, no sleep. Essentially the even iterations are doing the 2nd iteration and odd iterations are doing the 3rd iteration (just advancing numbers in each case).

That is what is happening. Why it is happening is because you set a fixed point in time and then use current time to compare it, mixing in a bit of sleeping as well. This effectively shifts your processing from working in the now to working in the past (or future, depending on your perspective) and then updating to work on the now again for the next loop (but still comparing your now value to the previous past value).


In reply to Re: while loop acting up, though I'm not sure how by SimonPratt
in thread while loop acting up, though I'm not sure how by msh210

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.