这是我正在尝试做的事情:
点击“输入姓名”键入姓名首字母(例如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>
答案 0 :(得分: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);
}