Your challenge is to make an infinite loading screen, that looks like this:
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 longLoading...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 code-golf, 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!
-
4\$\begingroup\$ Can submissions wait 0.25 seconds before initially displaying output? \$\endgroup\$ETHproductions– ETHproductions2016年11月27日 20:42:02 +00:00Commented Nov 27, 2016 at 20:42
-
2\$\begingroup\$ No, but thanks for mentioning that, I'll add it to the rules @ETHproductions \$\endgroup\$FlipTack– FlipTack2016年11月27日 20:43:09 +00:00Commented Nov 27, 2016 at 20:43
-
\$\begingroup\$ Is a trailing newline (after the animating symbol) acceptable? \$\endgroup\$Copper– Copper2016年11月27日 20:43:58 +00:00Commented Nov 27, 2016 at 20:43
-
\$\begingroup\$ Of course :) @Copper \$\endgroup\$FlipTack– FlipTack2016年11月27日 20:44:52 +00:00Commented Nov 27, 2016 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– FlipTack2016年12月15日 06:57:23 +00:00Commented Dec 15, 2016 at 6:57
112 Answers 112
Java 7, (削除) 121 (削除ここまで) 118 bytes
void f()throws Exception{for(int k=0;;k++){System.out.print("\rLoading...
"+"|/-\\".charAt(k%=4));Thread.sleep(250);}}
Gif animation:
C function, 73 bytes
i;f(){for(;write(1,"\rLoading... -\b\\\b|\b/",13+i%8);i++)usleep(1<<17);}
Tested on SystemResque-Cd 4.9.6 in this program:
#include <stdio.h>
#include <unistd.h>
/*
i;
f(){
for(
;
write(1, "\rLoading... -\b\\\b|\b/", 13+i%8);
i++
){
usleep(1<<17);
}
}
*/
i;f(){for(;write(1,"\rLoading... -\b\\\b|\b/",13+i%8);i++)usleep(1<<17);}
int main(){
f();
return 0;
}
compiled with gcc 4.9.4
-
\$\begingroup\$ Welcome to the site! \$\endgroup\$DJMcMayhem– DJMcMayhem2017年05月25日 22:22:39 +00:00Commented May 25, 2017 at 22:22
Japt, 38 bytes
@Oq Oo`LoÃHg... `+"|/-\\"gW°%4)1
a#úiU
How it works
@Oq Oo`LoÃHg... `+"|/-\\"gW°%4)1
a#úiU
@ Declare a function...
Oq that clears the screen,
Oo"Loading... " prints this string,
+"|/-\\"gW++%4 plus the spinner char (using a variable W),
)1 and finally returns 1
and implicitly store this function to U.
a Call U once (return 1 is needed for this)
#úiU and call U every 250 milliseconds.
By the nature of Japt syntax, it's impossible to call U() directly (well, it's possible using JS directive $...$ but it's too long after all.) So we use U.a() method that calls U with numbers from 0 to infinity until U returns true. If you omit )1 at the end of the first line and try running it, your browser will hang.
Without the initial U() call, the output window will show the implicit output (seemingly random integer value) before first showing the Loading... text.
Finally, a#úiU actually translates to U.a(250 .i(U)), which registers the 250ms loop first and then passes the return value (which happens to be undefined) to U.a (which accepts optional function argument, but seems to do nothing special with undefined).
Julia, 61 bytes
while [email protected]("\rLoading... "*["|/-\\"...])==sleep(1/4)end
Bespoke, 535 bytes
PUT XX:DIGITNINE BI
PUT XX:FOUR FIFTH
PUT XX:FOUR SEVENTH
PUT XXX:I BI FOUR
PUSH I H V
PUT XX:TRI BI
PUT XX:FOUR SEXTET
DO COPY
DO COPY
PUT XXX:I NUMBERZERO TRI
PUT XXX:I I NUMBERZERO
PUT XXX:I NUMBERZERO FIFTH
PUT XXX:I NUMBERZERO NUMBERZERO
PUT XX:DIGITNINE SEVENTH
PUT XXX:I I I
PUT XX:SEVENTH SEXTET
PUSH I
CONTROL WHILE
OUTPUT CH
DO COPY
CONTROL END
CONTROL DOWHILE
DO COPY
PUT XXXX:FIFTH I I I;STACKTOP QUOTIENTOF
PUSH FOUR STACKTOP MODULO
PUSH BI STACKTOP PLUS
DO COPYN
OUTPUT CH
PUSH INTEIGHT;OUTPUT CH
STACKTOP PLUSONE
DO COPY
A loading animation in a terminal, with the text "Loading... " and a clockwise-spinning line.
Note: at the speed that the Python-based Bespoke interpreter runs on my computer, a time interval close enough to 0.25 seconds happens every 5111 iterations of my program's main loop. Bespoke has no command for waiting a specific length of time.
(Also, the arithmetic I do on my counter will theoretically take longer and longer to do as it gets higher, because of the way ints work in Python. I can rewrite my program if this is an issue.)
Bash, 62 bytes
c='|/-\';for((;;)){ echo Loading... ${c:d++%4:1}^[M;sleep .25;}
where ^[ represents ESC (ASCII 0x1b), which typically you can get by pressing CtrlV and then ESC.
ESC M is RI, reverse linefeed.
If you don't care about running indefinitely, you can save 2 bytes by using a recursive function:
c='|/-\';f(){ echo Loading... ${c:d++%4:1}^[M;sleep .25;f;};f
JavaScript (ES6), 90 bytes
(F=(i=0)=>{(c=console).clear();c.log('loading... '+'|/-\\'[i]);setTimeout(F,250,-~i%4)})()
-
2\$\begingroup\$
iwill eventually overflow, since it just keeps going up without modulation. This can be fixed at no cost withc.log('loading... '+'|/-\\'[i]);setTimeout(F,250,-~i%4)\$\endgroup\$ETHproductions– ETHproductions2016年11月29日 01:58:33 +00:00Commented Nov 29, 2016 at 1:58 -
3\$\begingroup\$ @ETHproductions Good catch, the poor user would only have to wait a mere 71 billion years. \$\endgroup\$George Reith– George Reith2016年11月29日 12:17:59 +00:00Commented Nov 29, 2016 at 12:17
Node, 72 bytes (70 with literal)
c=0;setInterval(_=>console.log('\x1BcLoading... '+'/-\\|'[c=-~c%4]),250)
If you replace \x1B with the literal escape character you can cut another 2 bytes. You don't need to call anything.
-
1\$\begingroup\$ Would
setInterval(_=>console.log('\x1BcLoading... '+'/-\\|'[c=-~c%4]),c=250)work since c is being mod-ed by four every time? \$\endgroup\$Adalynn– Adalynn2016年11月29日 22:12:05 +00:00Commented Nov 29, 2016 at 22:12
Swift 3, 75 bytes
do{"|/-\\".characters.map{print("\u{001B}[2J\nLoading... \(0ドル)");sleep(1)}}
I need to use \u{001B} (ESCAPE) and [2J (clear screen) here because the old system API is deprecated in Swift 3 and I can't use \r on Mac/Linux. I would lose bytes if I had to use the length posix_spawn call or NSTask. I also hate having to access a string's CharacterView in order to map over each character. At least I saved some bytes by using do instead of a while loop.
Python 3, (削除) 86 (削除ここまで) 83 bytes
GIF to follow. Golfing suggestions welcome as this is still a little verbose. -3 bytes thanks to Flp.Tkc.
import time
i=1
while i:print(end="\rLoading... "+"/-\|"[i%4]);time.sleep(.25);i+=1
-
\$\begingroup\$ You can stick the
\rbefore the Loading to save the costly bytes forend=. Note that this solution is extremely similar \$\endgroup\$FlipTack– FlipTack2016年11月30日 17:40:43 +00:00Commented Nov 30, 2016 at 17:40 -
\$\begingroup\$ @Flp.Tkc I could have sworn that there were no other Python answers on here. Thanks for the heads up. Also, that tip doesn't work as Python 3's
printhas the defaultendof\nwhich screws up the updating. Thanks anyway. \$\endgroup\$Sherlock9– Sherlock92016年11月30日 18:03:47 +00:00Commented Nov 30, 2016 at 18:03 -
\$\begingroup\$ In that case you could do
print(end="\rLoading... "+"/-\|"[i%4])\$\endgroup\$FlipTack– FlipTack2016年11月30日 21:40:29 +00:00Commented Nov 30, 2016 at 21:40
F# (interactive), 81 bytes
async{while 1>0 do for c in"|/-\\"do printf"\rLoading... %O"c;do!Async.Sleep 250}
In order to run it in F# interactive, you have to pass it to Async.Start or Async.RunSynchronously.
For reference, a little longer non-async version:
while 1>0 do for c in"|/-\\"do printf"\rLoading... %O"c;System.Threading.Thread.Sleep 250
and a slightly outdated gif :)
-
1\$\begingroup\$ Welcome to PPCG. Nice answer :) \$\endgroup\$FlipTack– FlipTack2016年12月03日 22:26:23 +00:00Commented Dec 3, 2016 at 22:26
SmileBASIC, 57 bytes
@L CLS?"Loading... "+"|/-\"[I]:I=(I+1)MOD 4 WAIT 15 GOTO@L
Ungolfed:
@L 'loop start
CLS 'clear console
?"Loading... "+"|/-\"[I] 'construct our output and print
I=(I+1)MOD 4 'inc counter, MOD to prevent overflow
GOTO @L 'loop
SmileBASIC, (削除) 51 (削除ここまで) 46 bytes
CLS?"Loading... ";"|\-/"[3AND MAINCNT/15]EXEC.
PKod , 48 bytes ~ Non-Competing
Possibly the only way I can do this, as PKod only has one write-able variable
lL=oo=ao=do=io=no=go=.ooo =|oyw=/oyw=-oyw=\oywl<
Explanation: l - Clear screen and since its the first char, allow printing no operation (NOP) chars
L - NOP, thus print "L"
=oo=ao=do=io=no=go=.ooo - Set as certain chars and print them.
(space) - Another NOP, thus print a space
=|oyw - Set char as "|", print it, then wait a quarter of a second and remove it
=/oyw=-oyw=\oywl - Same as above, with different symbols to match the challenge
< - Go back to the start
"Gif" (more like mp4): https://i.gyazo.com/577dd164313a6b2e5dbf40249efb435d.mp4
You can see quote marks around the code in the console, thats because cmd tries to do stuff with my "<" and would return an error. It's just to nicely pass the code to the interpeter without cmd interfering.
-
\$\begingroup\$ Why is this non-competing? \$\endgroup\$The Fifth Marshal– The Fifth Marshal2020年09月16日 13:29:14 +00:00Commented Sep 16, 2020 at 13:29
Bash, 100 bytes
while [ 1 ]; do for i in `echo '|/-\' | grep -o .`; do printf $'\rLoading...'$i;sleep 0.25;done;done
This is not nicely golfed, so please tell me where I can improve here.
This does work, and has been tested on a Raspbian Raspberry Pi, an Amazon server, and an Ubuntu machine. This would not work on a Solaris machine because the sleep command on those systems cannot take inputs less than 1.
C#, (削除) 183 (削除ここまで) (削除) 180 (削除ここまで) (削除) 178 (削除ここまで) (削除) 170 (削除ここまで) (削除) 161 (削除ここまで) 140 bytes
I know a C# solution has already been posted, but this one is a fully functional console program (including usings) in less bytes!
Golfed
class P{static void Main(){for(int i=0;;){System.Console.Write("\rLoading... "+@"|/-\"[i=i++==3?0:i]);System.Threading.Thread.Sleep(250);}}}
Ungolfed
class P
{
static void Main()
{
for (int i = 0; ;)
{
System.Console.Write("Loading... "+@"|/-\"[i = i++ == 3 ? 0 : i]);
System.Threading.Thread.Sleep(250);
}
}
}
Yes, I'm probably very late, but I had to post it..!
EDIT: I figured someone else posted a 109 byte C# solution, oh well
EDIT 2: Thanks to the poster of the 109 byte C# solution, I managed to lose 3 more bytes by removing i<4 from my for loop, thanks!
EDIT 3: Removed C# 6 string interpolation and used good old + instead to save 2 more bytes.
EDIT 4: Not declaring a var for the animation characters anymore, instead I added them directly into the Write() method, saving another 8 bytes
EDIT 5: Removed the parameter string[]s from the Main method to save 9 bytes!
EDIT 6: Used carriage return instead of System.Console.Clear(), removed a using and moved the incrementing of i + the ternary inside of System.Console.Write(), all thanks to @CSharpie! (all this saved 21 bytes)
-
\$\begingroup\$ Instead of
Console.Clearjust use a carriage return infront of the string.c.Write("\rLoading... "+@"|/-\"[i]);. Then you probably can further reduce by getting rid of the using since you only needSystem.Console.Write("\rLoading... "+@"|/-\"[i]);You can put your ternary expression in there too, making itSystem.Console.Write("\rLoading... "+@"|/-\"[i=i++==4?0:i]);whilst also removing the i++ from the for-loop. \$\endgroup\$CSharpie– CSharpie2017年02月01日 20:13:21 +00:00Commented Feb 1, 2017 at 20:13 -
\$\begingroup\$ One small mistake, cant edit comments after 5 minutes so:
Console.Write("\rLoading... "+ @"|/-\"[i=i++==3?0:i]);resulting in 142 bytes. \$\endgroup\$CSharpie– CSharpie2017年02月01日 20:19:56 +00:00Commented Feb 1, 2017 at 20:19 -
\$\begingroup\$ Woah @CSharpie that's some awesome improvements, thanks alot! I'll apply some of them, but I will have to explain why I rolled back the carriage return (I added it earlier): imgur GIF this is happening when the console window isn't wide enough, somehow. Doesn't happen using
.Clear()\$\endgroup\$Mika– Mika2017年02月02日 07:44:48 +00:00Commented Feb 2, 2017 at 7:44 -
\$\begingroup\$ thats nothing you need to worry about. \$\endgroup\$CSharpie– CSharpie2017年02月02日 08:04:09 +00:00Commented Feb 2, 2017 at 8:04
-
\$\begingroup\$ @CSharpie Oh, really? I'll apply that as well then! Thank you very much :) I also discovered a weird exception happening using
.Clear()but I don't think this is the right place to discuss that, so I posted it on SO instead \$\endgroup\$Mika– Mika2017年02月02日 08:15:04 +00:00Commented Feb 2, 2017 at 8:15
Python 3, 94 bytes
Okay. First answer. EDIT: SAVED 8 BYTES EDIT 2: SAVED 1 BYTE EDIT 3: SAVED 11 BYTES EDIT 4: SAVED 3 BYTES EDIT: SAVED 5 BYTES EDIT: SAVED 2 BYTES
import time
while 1:
for f in 0,1,2,3:print("Loading...","|/-\\"[f],end="\r");time.sleep(.25)
Ungolfed:
import time
while True: # Loop forever
for f in [0, 1, 2, 3]: # Loop four times
print("Loading...", "|/-\\"[f], end="\r") # Print Loading... then the current frame
time.sleep(0.25) # Wait 0.25 seconds
-
1\$\begingroup\$ Welcome to PPCG! Nice answer, although there is a shorter python solution here. You might want to see tips for golfing in python so you can make your future answers shorter :) \$\endgroup\$FlipTack– FlipTack2016年12月23日 20:42:58 +00:00Commented Dec 23, 2016 at 20:42
-
\$\begingroup\$ I see you have a bit of extra whitespace particularly
f += 1can be replaced withf+=1andf >= 3can becomef>=3. Additionally0.25can be replaced with.25\$\endgroup\$2016年12月23日 20:52:21 +00:00Commented Dec 23, 2016 at 20:52 -
\$\begingroup\$ Also I think
acan be defined as the string"|/-\\"without any issue. And sinceais only referenced once you don't have to define it at all. \$\endgroup\$2016年12月23日 20:53:45 +00:00Commented Dec 23, 2016 at 20:53 -
1\$\begingroup\$ Another thing I noticed is the fourth line is the same as
f%=3. \$\endgroup\$2016年12月23日 20:55:53 +00:00Commented Dec 23, 2016 at 20:55 -
\$\begingroup\$
While True:is the same asWhile 1:\$\endgroup\$2016年12月23日 20:57:06 +00:00Commented Dec 23, 2016 at 20:57
PHP, 56 bytes
while(!usleep(25e4))echo"\rLoading... ",'\|/-'[@$i++%4];
JavaScript (ES6) + HTML, (削除) 77 (削除ここまで) 74 bytes
f=c=>setTimeout(f,250,-~c%4,o.value="Loading... "+"|/-\\"[~~c])
<input id=o
Try It
(f=c=>setTimeout(f,250,-~c%4,o.value="Loading... "+"|/-\\"[~~c]))()
<input id=o>
C++ (224)
This certainly isn't the shortest code to do this. I like doing coding challenges, so I'm posting it anyway.
#include <iostream>
#include <string>
#include <thread>
using namespace std;int main(){string s = "|/-\\";int i=0;cout<<"Loading... ";while(1){cout<<s[i];this_thread::sleep_for(chrono::milliseconds(99));cout<<"\b";i=++i%4;}}
-
\$\begingroup\$ You can save 2 bytes by removing the spaces around
=. \$\endgroup\$2018年01月12日 17:46:21 +00:00Commented Jan 12, 2018 at 17:46
><> with -t.03 flag, 33 bytes
'o!vooooo|\-/ ...gnidaoL
{[4<o8o:
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.
Python 3 (Windows), (削除) 112 (削除ここまで) (削除) 109 (削除ここまで) 108 bytes
import time,os
while 1:
for i in ['|','/','-','\\']:print("Loading... "+i);time.sleep(1/4);os.system('cls')
Python 3 (*nix), (削除) 111 (削除ここまで) 110 bytes
import time,os
while 1:
for i in ['|','/','-','\\']:print("Loading... "+i);time.sleep(1/4);os.system('clear')
Thanks @Dion for reducing 3 bytes in Windows version and add more info about *nix run!
-
1\$\begingroup\$ You could save 3 bytes by moving the last line onto the previous one \$\endgroup\$Dion– Dion2021年06月10日 10:14:23 +00:00Commented Jun 10, 2021 at 10:14
-
2\$\begingroup\$ You could also change
0.25to1/4to save a byte :p \$\endgroup\$Dion– Dion2021年06月10日 11:45:02 +00:00Commented Jun 10, 2021 at 11:45 -
1\$\begingroup\$ save a bunch of bytes with
for i in '|/-\\':print(end="\rLoading... "+i);time.sleep(1/4)\$\endgroup\$MarcMush– MarcMush2021年06月10日 16:09:15 +00:00Commented Jun 10, 2021 at 16:09
CLC-INTERCAL -psyscall, 356 bytes
Uses latest feature: syscall #9.
DO;1<-#12DO;1SUB#1<-#94DO;1SUB#2<-#317DO;1SUB#3<-#314DO;1SUB#4<-#2567DO;1SUB#5<-#377DO;1SUB#6<-#364DO;1SUB#7<-#248DO;1SUB#8<-#670DO;1SUB#9<-#188DO;1SUB#10<-#255DO;1SUB#11<-#255DO;1SUB#12<-#213DO;2<-#1DO:9<-#392¢#452DOCOMEFROM#9(4)DO;2SUB#1<-#122(2)DO;2SUB#1<-#126(1)DO;2SUB#1<-#15DO;2SUB#1<-#47DO.1<-#8DOCOMEFROM.1DOREADOUT;1+;2(666)DO.9<-#9(9)DO.1<-.1~#14
Running gif before fixing; spinning counterclockwise
Running CLC-INTERCAL program on Termux.
Ungolfed
DO;1<-#12 DONOTE ;1 <- "\rLoading... "
DO;1SUB#1<-#94
DO;1SUB#2<-#317
DO;1SUB#3<-#314
DO;1SUB#4<-#2567
DO;1SUB#5<-#377
DO;1SUB#6<-#364
DO;1SUB#7<-#248
DO;1SUB#8<-#670
DO;1SUB#9<-#188
DO;1SUB#10<-#255
DO;1SUB#11<-#255
DO;1SUB#12<-#213
DO;2<-#1 DONOTE ;2 is one of "|/-\"
DONOTE implicit .1<-#0 as iterator
DO:9<-#392¢#452 DONOTE const 250,000
DOCOMEFROM#9
(4)DO;2SUB#1<-#122 DONOTE /
(2)DO;2SUB#1<-#126 DONOTE -
(1)DO;2SUB#1<-#15 DONOTE \
DO;2SUB#1<-#47 DONOTE |
DO.1<-#8
DOCOMEFROM.1 DONOTE possibly from 4 2 or 1
DOREADOUT;1+;2
(666)DO.9<-#9 DONOTE sleep for :9 microseconds
(9)DO.1<-.1~#14 DONOTE right shift
YASEPL, 122 bytes
=sŔ,9=1)pipesdividesminuss"\"=b27ドル»sopenbracket=o|3`1!aŔ,9!c$a-s}1,250,3|`3!b~#"2J"~#"HLoading..."=g\o,1~!o+%4!Ŕ,9|
it starts automatically, the compiler just takes a second to load the file so thats why there is a pause if you run this
I cant exactly make a GIF of this right now, but when I can i'll update.
this uses ANSI.
Easyfuck, (削除) 41 (削除ここまで) 36 bytes
ymsĘ·^U¶ąĺw‹ž]€ ĎÁ␆n␈2 ́A•ŕ␐.\»>í90ďSOS
due to lack of unicode representations for c1 control characters, they have been replaced by their superscripted abbreviations
Decompressed:
<[.<U]<U[.<W<.<]@␈␙|␈␙\␈␙-␈␙/␀ ...gnidaoL
<[.<U]<U[.<W<.<]@␈␙|␈␙\␈␙-␈␙/␀ ...gnidaoL
< go to the last cell
[.<U] go through cells backwards, printing and deleting until encountering a null character
<U delete the null character
[.<W<.<] go backwards through characters looping when reaching the beginning, printing and waiting according to their values
@ marks the end of code
␈␙|␈␙\␈␙-␈␙/␀ ...gnidaoL initializer data
-
\$\begingroup\$ Is your code using raw EM and \ bytes, and you are using the unicode versions just for presentation purposes? If not, this is 59 bytes in UTF-8 \$\endgroup\$noodle person– noodle person2024年03月26日 11:06:20 +00:00Commented Mar 26, 2024 at 11:06
-
\$\begingroup\$ @noodleman they're raw bytes, i had to use powershell to push them into the file one by one and then i replaced them for presentation purposes \$\endgroup\$Quadruplay– Quadruplay2024年03月26日 12:23:33 +00:00Commented Mar 26, 2024 at 12:23
Uiua, 39 bytes
⍢(&sl1/4&pf⊂"\rLoading... "⊢.↻1)1$ |/-\
For some reason it looks really slow in the GIF but I can assure you on my computer it waits for 1/4 of a second. You may also notice that in the GIF, the code has an extra backslash; that's because I have Uiua 0.9.5 installed on my computer. In 0.10.0, the way $ strings work is a bit different, and the extra backslash isn't needed.
Explanation:
⍢(...)1$ |/-\
This starts with "|/-\" on the stack and does the rest of the program in a loop forever. A byte is saved by using the raw string syntax $ |/-\ instead of the normal "|/-\\".
↻1
Rotate this string one character to the left.
&pf⊂"\rLoading... "⊢.
Combine the first character of this with the loading text, and print it. The \r causes the text to be printed over whatever was already there.
&sl1/4
Sleep for a quarter of a second. Using a fraction literal like this is a byte shorter than 0.25.
GNU AWK, 86 bytes
Run it on your terminal:
gawk -ltime 'BEGIN{for(printf"Loading... |";sleep(1/4)++k;)printf"\b"substr("|/-\\",k%4+1,1)}'
80 chars of code + 6 chars for arguments
This is just GNU AWK relying on its time extension and respecting all rules. No input, no BASH, no coreutils. Could be shorter, but the sleep function must be called after the first printf.
gawk -ltime Runs GAWK with time extension.
Exactly the same as using @load "time", but shorter.
'
BEGIN{
for( Begins the loop without input.
printf"Loading... |"; Starts printing the first iteration without trailing line.
sleep(1/4)++k; Waits .25 second, which returns 0, and appends to the counting variable.
No big deal here. I just did it to save characters.
;)
printf
"\b" Prints a backspace
substr("|/-\\", appended to a sub-string of this monstrosity (\ is escaped)
k%4+1, starting at the character mod 4 + 1 (GAWK starts counting at 1)
1) with length 1.
'
Casio BASIC (fx-9750GIII), (削除) 64 (削除ここまで) 60 bytes
"Loading..."
Lbl A
1+Ans Rmdr 4
Locate 12,1,StrMid("\\|/-",Ans,1)
For 0→I To 200
Next
Goto A
The timing is probably not accurate since the Casio fx-9750giii doesn't have any built-in time functions as far as I know, but I think I got pretty close.
Timing ranges on what device you use this on, since it uses a for loop to separate the frames.
tcl, 83
while 1 {lmap c {| / - \\} {puts -nonewline \rLoading...$c;flush stdout;after 250}}
Can be seen running on: http://tpcg.io/_6PBTFX
Unfortunately, this online compiler can not demonstrate it going back rewriting the line. In a local machine it can be seen.
Explore related questions
See similar questions with these tags.