HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

The effect of typing a sentence out

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
effectthetypingsentenceout

Problem

The following code is complete and works perfectly. What I want to do to is make it more modular in a way where I can use the code again - similar to an api. I also hardly ever code in javascript, and I think my code looks disgusting, I feel like there are cleaner ways to write what I've written but I just don't have enough knowledge yet. If anyone has some advice/suggestion that would be appreciated.

The purpose of the module is to give the effect of a sentence being typed, then being backspaced out one character at a time. Here is a demo on my site: meneliktucker.me.

There are four main values to set:

  • sentences() - which is an array of sentences to be written out.



  • pause_sentence_time - the amount of time the sentence will pause after be written out - so the user can read the sentence before the sentence is deleted.



  • write_time - the time in ms to print a character



  • delete_time - the time in ms to delete a character



```
var delete_characters, delete_time, loop_characters,
loop_sentences, pause_sentence_time, print_char, sentences,
str_distance, write_time;

pause_sentence_time = 750;
write_time = 60;
delete_time = 40;

sentences = function() {
var all_sentences;
return all_sentences = ['is a student at the uOttawa.',
'loves to build things.'];
};

window.automatic_typing = function() {
var char_array;
char_array = sentences()[0].split('');
loop_characters(char_array, 0, 0);
};

loop_sentences = function(prev_index) {
var char_array, char_index, current_index;
current_index = (prev_index + 1) % sentences().length;
char_index = str_distance(prev_index);
char_array = sentences()[current_index].split('');
loop_characters(char_array, char_index, current_index);
};

loop_characters = function(char_array, char_index, index) {
var timer;
timer = 60;
if (char_index === char_array.length) {
timer = 750;
}
setTimeout((function() {
if (char_index === char_array.length) {
return delete_cha

Solution

Here is an update of what I've done:

I decided to make the code I provided earlier into a jQuery plugin. I reviewed jQuery plugins that I've used in the past then re-designed my code the JavaScript way. By using common feature in the JavaScript language such as the call method and utilizing object properties and prototypes.

I wrote it so you can call this jQuery plugin by simply entering in the element you want to have the effect apply to and then inputting your own customized values for the pause time, write time, delete time and sentence array.

Here is a sample of how to do that:

$('.automatic-typing').automaticTyping({
    writeTime   :  60,
    deleteTime  :  40,
    pauseTime   :  750,
    sentences   : ['Enter in a sentence here',
                  'Another here', 
                  '... As many as you want.'
                   ]
  });


Here is the re-designed code:

(function($) {
  "use strict";
  var methods = {
    init: function(options){

      options = $.extend({
        writeTime         :   60,
        deleteTime        :   60,
        pauseTime         :   1000,
        sentences         :   [
       'if you are seeing this',
       'you have not set your own values'
        ],
        charArray         :   [], //do not set this value and values below this point
        sentencesIndex    :   0,  
        charIndex         :   0,
        distance          :   0   
      }, options);

      this.each(function(){

        options = $.extend(true, {}, options); //deep copy the options object for every element
        options.charArray = options.sentences[0].split("");
        methods.loopCharacters.call(this, options);

      });

    },

    loopSentences: function(options){

      options.sentencesIndex = (options.sentencesIndex + 1) % options.sentences.length;
      options.charIndex = options.distance;
      options.charArray = options.sentences[options.sentencesIndex].split("");
      methods.loopCharacters.call(this, options);

    },

    loopCharacters: function(options){

      var i = options.charIndex;
      var len = options.charArray.length;
      var timer = options.writeTime;

      if (i === len) {
        timer = options.pauseTime;
      }

      setTimeout((function() {
        if (i === len) {
          methods.setDistance.call(this, options);
          return methods.deleteCharacters.call(this, options);
        }

        $(this).append(options.charArray[options.charIndex]);
        options.charIndex++;
        methods.loopCharacters.call(this, options);

      }).bind(this), timer);

    },

    deleteCharacters: function(options){

      setTimeout((function() {
        var currentSentence = $(this).text();

        if (currentSentence.length === options.distance) {
          return methods.loopSentences.call(this, options);
        }

        $(this).text(currentSentence.slice(0, currentSentence.length - 1));
        methods.deleteCharacters.call(this, options);

      }).bind(this), options.deleteTime);

    },

    setDistance: function(options){

      var nextSentenceIndex = options.sentencesIndex + 1;
      var cArr = options.charArray;
      var nextcArr = options.sentences[nextSentenceIndex % options.sentences.length].split('');
      var counter = 0;

      for (var i = 0; i <= cArr.length; i++) {
        if (cArr[i] === nextcArr[i]) {
      counter++;
        } else { break; }
      }

      if (cArr[counter] !== " " && counter !== 0) {
        while (true) { 
      if (cArr[--counter] === " ") { break; } 
        }
      }

      options.distance = counter;
    }

  };

  $.fn.automaticTyping = function(method){

    if ( typeof method === 'object' ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.');
    }

  };

})(jQuery);

Code Snippets

$('.automatic-typing').automaticTyping({
    writeTime   :  60,
    deleteTime  :  40,
    pauseTime   :  750,
    sentences   : ['Enter in a sentence here',
                  'Another here', 
                  '... As many as you want.'
                   ]
  });
(function($) {
  "use strict";
  var methods = {
    init: function(options){

      options = $.extend({
        writeTime         :   60,
        deleteTime        :   60,
        pauseTime         :   1000,
        sentences         :   [
       'if you are seeing this',
       'you have not set your own values'
        ],
        charArray         :   [], //do not set this value and values below this point
        sentencesIndex    :   0,  
        charIndex         :   0,
        distance          :   0   
      }, options);

      this.each(function(){

        options = $.extend(true, {}, options); //deep copy the options object for every element
        options.charArray = options.sentences[0].split("");
        methods.loopCharacters.call(this, options);

      });

    },

    loopSentences: function(options){

      options.sentencesIndex = (options.sentencesIndex + 1) % options.sentences.length;
      options.charIndex = options.distance;
      options.charArray = options.sentences[options.sentencesIndex].split("");
      methods.loopCharacters.call(this, options);

    },

    loopCharacters: function(options){

      var i = options.charIndex;
      var len = options.charArray.length;
      var timer = options.writeTime;

      if (i === len) {
        timer = options.pauseTime;
      }

      setTimeout((function() {
        if (i === len) {
          methods.setDistance.call(this, options);
          return methods.deleteCharacters.call(this, options);
        }

        $(this).append(options.charArray[options.charIndex]);
        options.charIndex++;
        methods.loopCharacters.call(this, options);

      }).bind(this), timer);

    },

    deleteCharacters: function(options){

      setTimeout((function() {
        var currentSentence = $(this).text();

        if (currentSentence.length === options.distance) {
          return methods.loopSentences.call(this, options);
        }

        $(this).text(currentSentence.slice(0, currentSentence.length - 1));
        methods.deleteCharacters.call(this, options);

      }).bind(this), options.deleteTime);

    },

    setDistance: function(options){

      var nextSentenceIndex = options.sentencesIndex + 1;
      var cArr = options.charArray;
      var nextcArr = options.sentences[nextSentenceIndex % options.sentences.length].split('');
      var counter = 0;

      for (var i = 0; i <= cArr.length; i++) {
        if (cArr[i] === nextcArr[i]) {
      counter++;
        } else { break; }
      }

      if (cArr[counter] !== " " && counter !== 0) {
        while (true) { 
      if (cArr[--counter] === " ") { break; } 
        }
      }

      options.distance = counter;
    }

  };

  $.fn.automaticTyping = function(method){

    if ( typeof method === 'object' ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.');
    }

  };

})(jQuery);

Context

StackExchange Code Review Q#87310, answer score: 3

Revisions (0)

No revisions yet.