Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Simulate 'backspace' in Jquery

I have a simple animation code, looks like a console input.

Originally from: https://codepen.io/atunnecliffe/pen/BaZyLR

I modified the splash screen intro into just a console input in my website:

Image of the code in my website

Code:

<script>
        //console
        var textarea = $('.term');
        var text = 'ping life';

        var i = 0;

        runner();

        function runner() {
            textarea.append(text.charAt(i));
            i++;
            setTimeout(
                function () {
                    runner();
                }, Math.floor(Math.random() * 1000) + 50);
        }
    </script>

Now the effect that I want is a bit complex, for me at least, as my knowledge about JQuery is limited. I wanted the code to enter ping life, then backspace completely, repeat infinitely. I looked up on how to simulate backspace in JQuery, using escape sequence of (8), but I am not sure how to use the escape sequence, nor implement the function into the existing recursive function, for it to repeat infinitely.

Any help would be wonderful :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Like this? Counting like this will give a zigzag like counting pattern. I added buffers for start and end of input, and a fixed timeout for deleting letters.

textarea.text(text.substr(0, i)) selects a substring of your text (treated as an array of letters - selecting everything between index 0 and i)

Easier than appending and deleting letters

var direction = 1;
var i = 0;
var textarea = $('.term');
var text = 'ping life';

// NOTE:
// I added the "@dev:~$ " as css:before elem, easier to write the code

function count() {
    i += direction;
    direction *= (((i % text.length) == 0) ? -1 : 1);
    textarea.text(text.substr(0, i));
    clearInterval(time);
    
    // direction is 1 if counting up
    if (direction === 1) {
        if (i === 0) {
            // buffer for start
            time = setInterval(count, 1000);
        } else {
            time = setInterval(count, Math.floor(Math.random() * 1000) + 50);
        }
    } else {
        // direction is -1 if counting down
        if (i === text.length) {
            time = setInterval(count, 1500);
        } else {
            // buffer for end
            time = setInterval(count, 100);
        }
    }
   
}

// inital interval
// setTimeout doesn't work well here
var time = setInterval(count, 1000)
html,
body {
  margin: 0 auto;
  height: 100%;
}

pre {
  padding: 0;
  margin: 0;
}

pre::before {
  content: "@dev:~$ ";
  color: white;
}

.load {
  margin: 0 auto;
  min-height: 100%;
  width: 100%;
  background: black;
}

.term {
  font-family: monospace;
  color: #fff;
  opacity: 0.8;
  font-size: 2em;
  overflow-y: auto;
  overflow-x: hidden;
  padding-top: 10px;
  padding-left: 20px;
}

.term:after {
  content: "_";
  opacity: 1;
  animation: cursor 1s infinite;
}

@keyframes cursor {
  0% {
    opacity: 0;
  }
  40% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="load">
  <pre class="term"></pre>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!