New Javascript user

Posts

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?

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.

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:

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

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.

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