这是我正在尝试做的事情:
通过单击“输入姓名”键入首字母(例如 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>