Reputation: 243
So I'm stuck on this script and am having trouble figuring out how to finish it. I need it to change the new_string to be the old string after its finished its animation and then with setInterval
after 1 or 2 min run the same animation from the new old string to a new string that I will grab from a JSON. Here is the script:
$(document).ready(function () {
var old_string = "first word";
var new_string = "second word";
while (old_string.length < new_string.length) {
old_string += " ";
}
while (new_string.length < old_string.length) {
new_string += " ";
}
for (i = 0; i < 18; i++) {
var cell_id = create_cell();
cycle_characters(old_string.charCodeAt(i), new_string.charCodeAt(i), cell_id);
}
});
var create_cell = function() {
var $rack = $("#rack");
var cell_id = "cell_" + $("#rack .cell").size() + 1;
$rack.append($("<span class='cell first_run'>").attr("id", cell_id));
return cell_id;
}
var cycle_characters = function (old, newer, cell_id) {
// 32 = space; 126 = tilde
// low-range ASCII only
var lower_limit = 32;
var upper_limit = 126;
var old = parseInt(old);
var newer = parseInt(newer);
if (old > upper_limit || old < lower_limit) {
old = lower_limit;
}
if (newer > upper_limit || newer < lower_limit) {
newer = lower_limit;
}
if ("string" != typeof cell_id) {
cell_id = $(cell_id).attr("id");
}
var $cell = $("#" + cell_id);
$cell.text(String.fromCharCode(old));
if (newer != old) {
var call = "cycle_characters(" + (old + 1) + ", " + newer + ", " + cell_id + ")";
if ($cell.hasClass("first_run")) {
$cell.removeClass("first_run");
setTimeout(call, 1000);
} else {
setTimeout(call, 20);
}
}
}
Here is also a JsFiddle
Upvotes: 3
Views: 83
Reputation: 132088
Here is an updated example that may be doing what you want:
Note, the setNewWord
function would obviously need to pull the next word from some other source besides a random word from an array like I am doing.
And for posterity, here is the code:
$(document).ready(function () {
var old_string = "First Word";
var new_string = "Second Word";
function doTheDo() {
$('#rack').html("");
while (old_string.length < new_string.length) {
old_string += " ";
}
while (new_string.length < old_string.length) {
new_string += " ";
}
for (i = 0; i < new_string.length; i++) {
var cell_id = create_cell();
cycle_characters(old_string.charCodeAt(i), new_string.charCodeAt(i), cell_id);
}
}
function setNewWord() {
// grab the word from some json
words = ['Abracadabra', 'Foo bar baz splat', 'Bingo Dingo Ringo']
new_string = words[Math.floor(Math.random() * words.length)]
doTheDo();
}
function create_cell() {
var $rack = $("#rack");
var cell_id = "cell_" + $("#rack .cell").size() + 1;
$rack.append($("<span class='cell first_run'>").attr("id", cell_id));
return cell_id;
}
function cycle_characters(old, newer, cell_id) {
// 32 = space; 126 = tilde
// low-range ASCII only
var lower_limit = 32;
var upper_limit = 126;
var old = parseInt(old);
var newer = parseInt(newer);
if (old > upper_limit || old < lower_limit) {
old = lower_limit;
}
if (newer > upper_limit || newer < lower_limit) {
newer = lower_limit;
}
if ("string" != typeof cell_id) {
cell_id = $(cell_id).attr("id");
}
var $cell = $("#" + cell_id);
$cell.text(String.fromCharCode(old));
var current = '';
$('.cell').each(function (el, i) {
current += $(i).text(); //console.info(i);
});
if (current != new_string && newer != old) {
if ($cell.hasClass("first_run")) {
$cell.removeClass("first_run");
setTimeout(function() {cycle_characters(old+1, newer, cell_id);}, 1000);
} else {
setTimeout(function() {cycle_characters(old+1, newer, cell_id);}, 20);
}
} else if (current == new_string) {
old_string = new_string;
setTimeout(function() { setNewWord();}, 3000);
}
}
doTheDo();
});
Upvotes: 2