Make 'cursor' blink and fadeOut using jquery for typewriter effect

I am using jquery to set up a typewriter effect on a div. I am not using css to do this because the sentence will be cover more than 1 line. The problem I am facing is getting the cursor to blink, then fade away when line is typed.

Html:

<div class="typewriter">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

JS

/*****Start Typewriter effect*****/
  $('.typewriter').css('display', 'none');
  setTimeout(function(){
    $('.typewriter').css('display', 'flex');

    var str = $('.typewriter').html() + 1,
    i = 0,
    isTag,
    text,
    cursor = "|",
    timer;

    (function type() {
      text = str.slice(0, ++i);
      if (text === str){ 
        return;
      }
      $('.typewriter').html(text + " " + cursor);
      timer = setTimeout(type, 40);
    }());
  }, 300);
  /*****End Typewriter effect*****/

Here is a jsfiddle https://jsfiddle.net/ht4569wv/

Just validate when the effect you already made is done and set up another timer to blink the cursor:

/*****Start Typewriter effect*****/
  $('.typewriter').css('display', 'none');
  setTimeout(function(){
    $('.typewriter').css('display', 'flex');

    var str = $('.typewriter').html(),
    i = 0,
    isTag,
    text,
    cursor = "|",
    timer;

    (function type() {
      text = str.slice(0, ++i);
      if (text === str){ 
        i = 0;
        blink();
        return;
      }
      $('.typewriter').html(text + " " + cursor);
      timer = setTimeout(type, 40);
    }());

    function blink() {
      i++;
      const foo = str + " " + (i%2 ? cursor : '');
      $('.typewriter').html(foo);
      if (i < 10) timer = setTimeout(blink, 600);
      else fade();
    }

    function fade() {
        $('.typewriter').html(str);
    }

  }, 300);
  /*****End Typewriter effect*****/

demo: https://jsfiddle.net/y2s3fv6d/

Make, verb (used with object), made, mak�ing. to bring into existence by shaping or changing material, combining parts, etc.: to make a dress;� Make definition is - to bring into being by forming, shaping, or altering material : fashion. How to use make in a sentence.

I have slightly changed your approach, and with the help of CSS I have created a blinking cursor.

here is the JSfiddle: https://jsfiddle.net/mkbctrlll/65ay3q8o/72/

JS:

var $typewriter = $('.typewriter')

/*****Start Typewriter effect*****/
setTimeout(function() {

  console.log('Start!')
  $typewriter.css('display', 'block');

  var str = $typewriter.html() + 1,
    i = 0,
    isTag,
    text,
    cursor = document.createElement('span'),
    timer;

  cursor.classList.add('cursor');


  (function type() {
    text = str.slice(0, ++i);

    if (text === str) {
        console.log('Done')

      setTimeout(function() {
        $(cursor).addClass('hidden')
      }, 2000);
      return;
    }

    $typewriter.html(text + " ");
    $typewriter.append(cursor)

    timer = setTimeout(type, 0);

  }());
}, 300);

/*****End Typewriter effect*****/

CSS:

.typewriter {
  display: none;
  overflow: hidden;
  /* Ensures the content is not revealed until the animation */
}

.cursor {
  transition: opacity 0.6s;
  border-right: .15em solid orange; /* The typwriter cursor */
  animation: blink-caret .5s step-end infinite;
}

.cursor.hidden {
   opacity: 0
}

/* The typewriter cursor effect */

@keyframes blink-caret {
  from,
  to {
    border-color: transparent
  }
  50% {
    border-color: orange
  }
}

Make, We're going live right now! Come join us and #maketogether ! www.facebook.com/makemaga… Make definition, to bring into existence by shaping or changing material, combining parts, etc.: to make a dress; to make a channel; to make a work of art. See more.

Use my .typeText function.

//<![CDATA[
/* external.js */
$(function(){ // jQuery onload
$.fn.extend({
  typeText:function(interval){
    var t = this, s = this.text().split(''), ti, i = 0;
    this.text(s[0]+'|');
    ti = setInterval(function(){
      t.text(t.text().replace(/\|$/, ''));
      if(s[++i]){
        t.append(s[i]+'|');
      }
      else{
        clearInterval(ti);
      }
    }, interval);
  }
});
$('.typeview').css('display', 'block').each(function(i, e){
  $(e).typeText(50);
});
}); // jQuery onload end
//]]>
/* external.css */
*{
  box-sizing:border-box; padding:0; margin:0;
}
html,body{
  width:100%; height:100%;
}
body{
  background:#ccc;
}
#content{
  padding:10px;
}
.typeview{
  display:none; text-align:justify; background:#fff; padding:8px 10px;
}
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1' />
    <title>Test Template</title>
    <link type='text/css' rel='stylesheet' href='external.css' />
    <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
    <script type='text/javascript' src='external.js'></script>
  </head>
<body>
  <div id='content'>
    <div class='typeview'>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
  </div>
</body>
</html>

Make: ✓, Get your #game on! @ScrappyCircuits is starting @ 10am PT / 1pm ET to make a game board with some junk drawer magic! WATCH LIVE on @make FB or #makercamp! makercamp.com/event/scrap… 170 synonyms of make from the Merriam-Webster Thesaurus, plus 282 related words, definitions, and antonyms. Find another word for make. Make: to bring into being by combining, shaping, or transforming materials.

MAKE (verb) definition and synonyms, Join us for a first look of @josefprusa 's latest printer. All the prusa bells and whistles, a fraction of the cost. www.youtube.com/watch?v=P… Make enables the end user to build and install your package without knowing the details of how that is done -- because these details are recorded in the makefile that you supply. Make figures out automatically which files it needs to update, based on which source files have changed.

Make, Plan C Live: Impacts of COVID-19 in Northwest Louisiana www.pscp.tv/w/cg201TMzMTQ… Dozens of projects in every issue covering Robots, Drones, 3D printing, craft and more; Tips and skill-building tutorials with inspiration from the leaders of the maker community

Introduction (GNU make), We're going live right now! Come join us and #maketogether ! www.facebook.com/makemaga… You need to enable JavaScript to run this app.

Comments
  • Why exactly can you not use CSS? If you wrap the | character in a span and make the span blink with css, it should work no matter how many lines the text has. The span is an inline element. It should flow right along with the text.
  • In your solution you forget to fade away the cursor once the writing is done. Nice idea btw.
  • Isn't the point to make it blink when it isn't typing? Is it supposed to fade out after a set amount of time or something?
  • This is what I think is expected, but let's wait for author to confirm
  • I added this in to my answer since it was trivial and just as trivial to remove if he doesn't need it.
  • Awesome! Thanks for the help. Yes, I need the cursor to blink then fade away. Thanks for the quick response!