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 characterdelete_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_characters(index, str_distance(index));
}
print_char(char_array[char_index++]);
loop_characters(char_array, char_index, index);
}), timer);
};
print_char = function(c) {
$('.automatic-typing').append(c);
};
delete_characters = function(index, dist) {
setTimeout((function() {
var temp;
temp = $('.automatic-typing').text();
if (temp.length === dist) {
return loop_sentences(index);
}
$('.automatic-typing').text(temp.slice(0, temp.length - 1));
delete_characters(index, dist);
}), 40);
};
//The purpose of this function is to count the number of letters that
//the current and next sentence share from the start of both sentences.
//ex. the distance between 'this is cool' and 'this is not cool' is 7.
str_distance = function(index) {
var counter, i, next_index, next_str_array, str_array, _i, _ref;
str_array = sentences()[index].split('');
next_index = index + 1;
next_str_array = sentences()[next_index % sentences().length].split('');
counter = 0;
for (i = _i = 0, _ref = str_array.length; _i <= _ref; i = _i += 1) {
if (str_array[i] === next_str_array[i]) {
counter++;
} else {
break;
}
}
if (str_array[counter] !== " " && counter !== 0) {
while (true) {
if (str_array[--counter] === " ") {
break;
}
}
}
return counter;
};