New Javascript user

by Zenovis

Zenovis
Junior Member
Kohai
Posts: 1
Threads: 1
Joined: Dec 2015
Reputation: 0
12-18-2015, 10:32 AM
#31575 (1)
Hello, I am a fairly new programmer and am learning the basics of javascript. I would appreciate anyone that could help me with this issue. I am trying to make a simple game in the console, however I am having trouble making a script that would go something like this:
Waiting
Waitng.
Waiting..
Waiting...
Waiting
Etc, on the same line but deletes the dots. Afterwards I would like it to completely erase the "waiting", but I want to keep the rest of the console log there. Can anyone help me?
Kiss of Death
Deliciously Salty
Nephilim
Posts: 541
Threads: 72
Joined: Jun 2015
Reputation: 21
12-18-2015, 11:05 AM
#31581 (2)
I'm no pro at javascript either, but I have an idea on how to do it.
Set a div and give it an ID, for example
<div id="waiting"></div>

Add two variables, one for milisecond and the other to add this aspect to your function. So, for instance:
var ms=100;
var somethingVar = setInterval(function() {functionName()}, ms);

Then, what you want to do is in <script>, make a new function that runs on the load of the page (can be done under <head onload="functionName()">). Like so:
function functionName(somethingVar){
}

Inside it, you just gotta make a for loop that changed the innerHTML of your div you set earlier. For instance:
for(var i=0;i<5,i>0;i++)
{
if(i=1){
document.getElementById("waiting").innerHTML = "Waiting";
}
if(i=2){
document.getElementById("waiting").innerHTML = "Waiting.";
}
if(i=3){
document.getElementById("waiting").innerHTML = "Waiting..";
}
if(i=4){
document.getElementById("waiting").innerHTML = "Waiting...";
i=0;
}
}

That's all in terms of waiting. Somebody might have a better idea; likewise, my code might not work, so try and pester a little around it, but you get a general idea of how it should look.

Akiba
lewdest of them all
Music
Posts: 49
Threads: 5
Joined: Nov 2015
Reputation: 2
12-18-2015, 11:31 AM (This post was last modified: 12-18-2015, 11:36 AM by Akiba. Edit Reason: let apparently still isn't supported)
#31586 (3)
Javascript pro to the rescue.

afaik, there's no way to delete text in the browser. Unless you're using node, of course, then you can do plenty.

This is the simple way:

Code:
var stdout = process.stdout;

// instead of console.log(str);
stdout.write(str);

// when you want to clear the line
stdout.clearLine();
stdout.cursorTo(0);
// your cursor has been returned to the beginning of the line, which is now blank


Another option is to use this nifty framework called blessed. https://github.com/chjj/blessed
Akiba
lewdest of them all
Music
Posts: 49
Threads: 5
Joined: Nov 2015
Reputation: 2
12-18-2015, 11:42 AM
#31587 (4)
Code:
var stdout = process.stdout;

var dot = 0;

var print = () => {
   stdout.clearLine();
   stdout.cursorTo(0);
   if (dot++ < 3) {
       stdout.write('Waiting' + '.'.repeat(dot));
       setTimeout(print, 500);
   }
}

print();

Wrote the whole thing up for you.
nerd
the nigga ya love to hate
Nyandere
Posts: 766
Threads: 40
Joined: Jul 2015
Reputation: 18
12-18-2015, 11:52 AM
#31588 (5)
(12-18-2015, 11:05 AM)Nanashi Wrote: I'm no pro at javascript either, but I have an idea on how to do it.
Set a div and give it an ID, for example
<div id="waiting"></div>

Add two variables, one for milisecond and the other to add this aspect to your function. So, for instance:
var ms=100;
var somethingVar = setInterval(function() {functionName()}, ms);

Then, what you want to do is in <script>, make a new function that runs on the load of the page (can be done under <head onload="functionName()">). Like so:
function functionName(somethingVar){
}

Inside it, you just gotta make a for loop that changed the innerHTML of your div you set earlier. For instance:
for(var i=0;i<5,i>0;i++)
{
if(i=1){
document.getElementById("waiting").innerHTML = "Waiting";
}
if(i=2){
document.getElementById("waiting").innerHTML = "Waiting.";
}
if(i=3){
document.getElementById("waiting").innerHTML = "Waiting..";
}
if(i=4){
document.getElementById("waiting").innerHTML = "Waiting...";
i=0;
}
}

That's all in terms of waiting. Somebody might have a better idea; likewise, my code might not work, so try and pester a little around it, but you get a general idea of how it should look.

he wants to do it in the console