坚持数字和字母随机javascript

时间:2013-03-23 00:29:37

标签: javascript jquery

所以我坚持使用这个脚本,并且无法弄清楚如何完成它。我需要它在完成动画后将new_string更改为旧字符串,然后在1或2分钟后使用setInterval将新旧字符串中的相同动画运行到我将从JSON中获取的新字符串。这是脚本:

$(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);
        }
    }

}

这也是JsFiddle

1 个答案:

答案 0 :(得分:2)

这是一个可能正在执行您想要的更新示例:

http://jsfiddle.net/ktEJy/2/

注意,setNewWord函数显然需要从像我正在做的数组中的随机单词之外的其他来源中提取下一个单词。

对于后代,这是代码:

$(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();
});