0

这是我正在尝试做的事情:

通过单击“输入姓名”键入首字母(例如 MS、AK、LT)。这保存了一个字符串,然后我将其转换为一个数组 (nameArray) 以获取每组首字母。在随机重新排序这些之后,我想将一些首字母放入 textareas 中,但这就是问题所在。

出了什么问题:

缩写显示片刻,然后在函数执行后消失。(另外,我正在尝试让一个 div(带有文本“随机化...”)以其他方式隐藏,显示自身 4 秒(4000 毫秒),同时重新排序首字母以表明这一点。这就是 setTimeout是为了……但这也不起作用。div 与文本一起消失)。为什么这些仅与功能的执行相协调?

这是JS代码:

var nameArray;

window.onload = pageLoad;

function pageLoad() {
    $("#randomizingNotification").hide();
    $("#prev_arrow").click(prevUser);
    $("#next_arrow").click(nextUser);
    $("#enter_names").click(orderNames);    
}

function orderNames() {
    nameArray = getNames();
    randomizeNames();
    displayNames();
}

function getNames() {
    var initialsString = prompt("Please enter initials, separated by a comma (e.g LK,AS,NM)");
    nameArray = initialsString.split(",");
    return nameArray;
}

function randomizeNames() {
    $("#randomizingNotification").show();
    var timer = setTimeout(function(){randomize(nameArray);},4000);
    $("#randomizingNotification").hide();
    clearTimeout(timer);
}

function randomize(array) {
    for (var i = 0; i < array.length; i++ ) {
        var randNum = Math.floor(array.length*Math.random()) //random number between 0 and length of array (rounded down
        var temp = array[i];
        array[i] = array[randNum];
        array[randNum] = temp;
    }
}

function displayNames() {
    var curr, up, prev, current, upcoming, previous;
    curr = 0;
    up = 1;
    prev = null

    current = nameArray[curr];
    upcoming = nameArray[up];

    $("#upcoming_pick").val(upcoming);
    $("#current_pick").val(current);
}

以下是相关的 HTML 代码:

<body>
<div id="header">
    <div id="randomizeNotContDiv">
        <div id="randomizingNotification">randomizing...</div>
    </div>

    <div id="page_title"><h1>Welcome to Classtech Shift Scheduler!</h1></div>

    <div id="helper_functions_div">
        <div id="enter_names_div">
            <a href="" id="enter_names">Enter Names</a>
        </div>
   </div>

<div id="main_content">
    <div id="name_tracker">

        <div><a href="" alt="prev" id="prev_arrow"><img src="Images/prev_arrow.png"/></a></div>

        <textarea name="upcoming_pick" cols="10" rows="1" class="picker_names" id="upcoming_pick"></textarea>
        <textarea name="current_pick" cols="10" rows="1" class="picker_names" id="current_pick"></textarea>
        <textarea name="previous_pick" cols="10" rows="1" class="picker_names" id="previous_pick"></textarea>

        <div><a href="" alt="next" id="next_arrow"><img src="Images/next_arrow.png"/></a></div>

    </div>
4

1 回答 1

0

你至少有几个问题,但主要问题是你的 setTimeout 的结构。setTimeout 类似于 AJAX 调用,因为它是非阻塞的。您传递给它的函数内部的任何内容都只会在计时器完成时执行,而在它之后的代码将立即执行。

我已经重新组织了您的随机化消息的隐藏和 displayNames 函数以进入 setTimeout 函数并且一切正常。

这是一个小提琴:http: //jsfiddle.net/nate/LB6dz/

这是代码:

var nameArray;

window.onload = pageLoad;

function pageLoad() {
    $("#randomizingNotification").hide();
    $("#enter_names").click(orderNames);    
}

function orderNames(event) {
    // Prevent the link's default action
    event.preventDefault();

    nameArray = getNames();
    randomizeNames();
}

function getNames() {
    var initialsString = prompt("Please enter initials, separated by a comma (e.g LK,AS,NM)");
    nameArray = initialsString.split(",");
    return nameArray;
}

function randomizeNames() {
    $("#randomizingNotification").show();
    var timer = setTimeout(function (){
        randomize(nameArray);
        // These items need to be inside the timeout, so they only run once it's done
        $("#randomizingNotification").hide();
        displayNames();
    }, 4000);
    // No need to clearTimeouts after they're done... they only run once
    // clearTimeout(timer);
}

function randomize(array) {
    for (var i = 0; i < array.length; i++ ) {
        var randNum = Math.floor(array.length*Math.random()) //random number between 0 and length of array (rounded down
        var temp = array[i];
        array[i] = array[randNum];
        array[randNum] = temp;
    }
}

function displayNames() {
    var curr, up, prev, current, upcoming, previous;
    curr = 0;
    up = 1;
    prev = null

    current = nameArray[curr];
    upcoming = nameArray[up];

    $("#upcoming_pick").val(upcoming);
    $("#current_pick").val(current);
}
于 2013-04-01T16:50:06.023 回答