114
\$\begingroup\$

Your challenge is to make an infinite loading screen, that looks like this:

enter image description here


Or, to be more specific:

  • Take no input.
  • Output Loading..., with a trailing space, but no trailing newline.
  • Infinitely cycle through the chars |, /, - and \: every 0.25 seconds, overwrite the last one with the next in the sequence. You can overwrite just the last character, or delete and rewrite the whole line, as long Loading... remains unchanged.

Rules

  • The output text must look exactly as specified. Trailing newlines/spaces are acceptable.
  • You should not wait 0.25 seconds before initially showing output - the first frame should be printed as soon as the program is run.
  • Your program should be able to run indefinitely. For example, if you use a counter for frames, the counter should never cause an error by exceeding the maximum in your language.
  • Although the waiting period between each "frame" should be 0.25 seconds, obviously this will never be exact - an error margin of 10% or so is allowed.
  • You may submit a function, but it must print to stdout.
  • You can submit an answer in a non-console (but still text-based) environment, as long as it is capable of producing the loading animation.
  • This is , so the shortest solution (in bytes) wins. Standard code-golf loopholes apply.
  • If possible, please provide a gif of your loading screen in action.

Example

Here is the C++ code I used to create the example (ungolfed):

#include <iostream>
#include <string>
#include <thread>

using namespace std;

int main() {
    string cycle = "|/-\\";
    int i = 0;

    cout << "Loading... ";

    while (true) {
        // Print current character
        cout << cycle[i];

        // Sleep for 0.25 seconds
        this_thread::sleep_for(chrono::milliseconds(250));

        // Delete last character, then increase counter.
        cout << "\b";
        i = ++i % 4;
    }
}

May the best golfer win!

\$\endgroup\$
19
  • 4
    \$\begingroup\$ Can submissions wait 0.25 seconds before initially displaying output? \$\endgroup\$ Nov 27 '16 at 20:42
  • 2
    \$\begingroup\$ No, but thanks for mentioning that, I'll add it to the rules @ETHproductions \$\endgroup\$
    – FlipTack
    Nov 27 '16 at 20:43
  • \$\begingroup\$ Is a trailing newline (after the animating symbol) acceptable? \$\endgroup\$
    – Copper
    Nov 27 '16 at 20:43
  • \$\begingroup\$ Of course :) @Copper \$\endgroup\$
    – FlipTack
    Nov 27 '16 at 20:44
  • 1
    \$\begingroup\$ @TheBitByte it means that, theoretically, nothing inside your program will cause it to error - such as a counter overflowing or reaching maximum recursion depth. \$\endgroup\$
    – FlipTack
    Dec 15 '16 at 6:57

95 Answers 95

1 2 3
4
0
\$\begingroup\$

><> with -t.03 flag, 33 bytes

'o!vooooo|\-/ ...gnidaoL
{[4<o8o:

Try it online!

The only part of the specification this doesn't implement is that the first frame should be printed as soon as the program is run which seems rather strict and unobservable. The first frame here take 1.08 seconds to print, and every frame after that takes 0.24 seconds.

\$\endgroup\$
0
\$\begingroup\$

Yabasic, 83 bytes

An anonymous function that takes no input and outputs to the console in graphics mode. Does not function with TIO.

Clear Screen
?"Loading..."
Do
?@(1,11)Mid$("|/-\\",i+1,1)
Wait.25
i=Mod(i+1,4)
Loop
\$\endgroup\$
2
  • \$\begingroup\$ Can you use i instead of i+1 in Mid$? \$\endgroup\$
    – 12Me21
    May 17 '18 at 11:55
  • \$\begingroup\$ @12Me21 Unfortunately not - Mid$ is 1-indexed in Yabasic, so the +1 is necessary. You can see this in the context of this question here \$\endgroup\$ May 17 '18 at 12:43
0
\$\begingroup\$

Groovy, 60 bytes 59 bytes, 61 bytes

print'Loading...';for(;;)'|/-\\'.any{print"$it\b";sleep 250}

(modified to work with later versions of groovy, removed ascii cast)

\$\endgroup\$
0
0
\$\begingroup\$

Stax, 24 bytes

Ñ!Δ╚«&╖ô^ô◙⌠≈%<╗∞█z9ÖºOT

Run and debug it

Delays 15 frames between each change, which at default 60fps should be equal to 1/4th of a second.

Explanation

W`lAtv>!`"|/-\"{[+p{|,}15*|:F
W                             loop forever
 `lAtv>!`                     push "Loading... "
         "|/-\"{            F for each char in "|/-\"
                [+p           add to Loading and print
                   {|,}15*    delay 1 frame 15 times
                          |:  clear the screen
\$\endgroup\$
4
  • \$\begingroup\$ limiting your display to 30fps would mess with any of the programs here. \$\endgroup\$
    – Razetime
    Jun 10 at 2:57
  • \$\begingroup\$ Hm. An error margin is allowed, and no fps requirements are specified. It should work fine on any modern browser. \$\endgroup\$
    – Razetime
    Jun 10 at 3:00
  • \$\begingroup\$ I will specify a 60fps browser requirement then. \$\endgroup\$
    – Razetime
    Jun 10 at 3:03
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$
    – Razetime
    Jun 10 at 3:05
0
\$\begingroup\$

05AB1E, 26 bytes

[13甲½... ”"|/-\"NèJ?Ƶ–.W

[13ç”...”"..."NèJ?Ƶ–.W  # trimmed program
[                       # forever...
   ç                    # push character with codepoint...
 13                     # literal
    ”...”               # push "Loading... "
               è        # push character in...
         "..."          # literal...
               è        # at index...
              N         # current index in loop...
               è        # modulo length of...
         "..."          # literal
                 ?      # output...
                J       # joined stack...
                 ?      # without trailing newline
                    .W  # wait...
                  Ƶ–    # 250...
                    .W  # milliseconds
\$\endgroup\$
1 2 3
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.