【发布时间】:2019-06-16 00:04:18
【问题描述】:
请给我一个普通的 JS 解决方案,因为我是编码新手,引入库只会让我更加困惑。
我在程序中有两个函数: changeText 包含异步 setTimeout 函数,可在 X 秒内淡入和淡出文本,以及 userNameinput,允许用户输入文本输入,然后在浏览器上显示输入。
我遇到的问题是 usernameinput 与 changeText 函数一起执行。我的目标是先执行 changeText 函数并完成,然后让 userNameInput(出现文本输入行)立即执行。
正如您在我的代码中看到的,我已经实现了一个回调来尝试解决这个问题。我创建了一个名为welcome 的新函数,将changeText 和useNameInput 函数捆绑在一起,这样当调用welcome 时,它将首先执行changeText,完成然后调用回调中打包的userNameInput。不知何故,我相信由于 changeText 函数中的 setTimeout 函数在 Javascript 环境之外的队列中放置了 X 时间,JS 看到堆栈中没有任何内容,因此无需等待即可继续执行 usernameInput。请帮忙!被困太久了!提前致谢。
HTML:
<div id="h1">Hello,<br></div>
<div id="inputDiv"></div>
CSS:
#h1{
opacity: 0;
transition: 1s;
}
JS:
function fadeIn() {
document.getElementById('h1').style.opacity = '1';
}
function fadeOut() {
document.getElementById('h1').style.opacity = '0';
}
var dialogue = ['Hello,', 'My name is Jane.', 'I have a dog!', 'What is your name?'];
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("value", "");
input.setAttribute("placeholder", "Type your name then press Enter");
input.setAttribute("maxLength", "4");
input.setAttribute("size", "50");
var parent = document.getElementById("inputDiv");
parent.appendChild(input);
parent.style.borderStyle = 'solid';
parent.style.borderWidth = '0px 0px .5px 0px';
parent.style.margin = 'auto';
function changeText() {
var timer = 0;
var fadeOutTimer = 1000;
for (let i = 0; i < dialogue.length; i++) {
setTimeout(fadeIn, timer);
setTimeout(fadeOut, fadeOutTimer);
setTimeout(function () {
document.getElementById('h1').innerHTML = dialogue[i];
}, timer);
timer = (timer + 3000) * 1;
fadeOutTimer = (fadeOutTimer + 3000) * 1.1;
console.log(timer, fadeOutTimer);
}
}
function welcome(callback) {
changeText();
callback();
}
welcome(function () {
function userNameInput() {
function pressEnter() {
var userName = input.value;
if (event.keyCode == 13) {
document.getElementById('h1').innerHTML = "Nice to meet you" +
" " + userName + "!";
}
}
input.addEventListener("keyup", pressEnter);
}
userNameInput();
});
【问题讨论】:
标签: javascript asynchronous callback synchronization