卡住数字和字母随机化javascript

Stuck on number and letter randomization javascript

本文关键字:随机化 javascript 数字      更新时间:2023-09-26

所以我被困在这个脚本上,我很难弄清楚如何完成它。我需要它改变new_string为旧字符串后,它完成了它的动画,然后与setInterval 1或2分钟后运行相同的动画从新的旧字符串到一个新的字符串,我将从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

这里是一个更新的例子,可能是你想要的:

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