JavaScript跳过document.write去window.prompt?
JavaScript skips over document.write to go to window.prompt?
这段代码本应在 window.prompt() 和 document.write() 之间来回切换,但它会遍历所有 window.prompt() 消息并迭代必须这样做,然后它会在 while 循环的出口处转储所有 document.write 输出。
来回可以做什么?
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>gasMilage</title>
<script type = "text/javascript">
var gasNow;
var distanceNow;
var mpgNow;
var gasOverall;
var distanceOverall;
var mpgOverall;
var answer;
answer = "yes";
gasOverall = parseInt(0);
distanceOverall = parseInt(0);
while(answer == "yes"){
gasNow = window.prompt(
"Enter the gallons of gas used: ")
gasNow = parseInt(gasNow);
distanceNow = window.prompt(
"Enter the distance traveled in miles: ")
distanceNow = parseInt(distanceNow);
mpgNow = parseInt(distanceNow / gasNow);
document.writeln(
"Your current mpg is " + mpgNow + "<br />");
gasOverall = gasNow + gasOverall;
distanceOverall = distanceNow + distanceOverall;
mpgOverall = parseInt(distanceOverall/gasOverall);
document.writeln(
"Your overall mpg is " + mpgOverall + "<br />");
answer = window.prompt(
"If you want to add more values, type yes.")
}
</script>
</head>
<body>
</body>
</html>
是的,这是因为提示出现得太快,DOM 没有时间写,因为有一个待处理的提示请求正在进行。解决方案是在您的提示方法上设置超时。
但是因为你在这里做了一个 while 循环,它会再次做第一个提示而不用等待超时。因此,与其使用 while 循环,不如将其包装在一个函数中,如果最后一个提示为是,则递归调用它。
试试这个 fiddle
(function query() {
gasNow = window.prompt(
"Enter the gallons of gas used: ")
gasNow = parseInt(gasNow);
distanceNow = window.prompt(
"Enter the distance traveled in miles: ")
distanceNow = parseInt(distanceNow);
mpgNow = parseInt(distanceNow / gasNow);
document.writeln(
"Your current mpg is " + mpgNow + "<br />");
gasOverall = gasNow + gasOverall;
distanceOverall = distanceNow + distanceOverall;
mpgOverall = parseInt(distanceOverall/gasOverall);
document.writeln(
"Your overall mpg is " + mpgOverall + "<br />");
setTimeout(() => {
answer = window.prompt(
"If you want to add more values, type yes.")
if(answer === 'yes') query();
}, 1);
})()
在每个 document.write 之后您可以添加一个延迟。这将为呈现文档留出时间。
这段代码本应在 window.prompt() 和 document.write() 之间来回切换,但它会遍历所有 window.prompt() 消息并迭代必须这样做,然后它会在 while 循环的出口处转储所有 document.write 输出。 来回可以做什么?
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>gasMilage</title>
<script type = "text/javascript">
var gasNow;
var distanceNow;
var mpgNow;
var gasOverall;
var distanceOverall;
var mpgOverall;
var answer;
answer = "yes";
gasOverall = parseInt(0);
distanceOverall = parseInt(0);
while(answer == "yes"){
gasNow = window.prompt(
"Enter the gallons of gas used: ")
gasNow = parseInt(gasNow);
distanceNow = window.prompt(
"Enter the distance traveled in miles: ")
distanceNow = parseInt(distanceNow);
mpgNow = parseInt(distanceNow / gasNow);
document.writeln(
"Your current mpg is " + mpgNow + "<br />");
gasOverall = gasNow + gasOverall;
distanceOverall = distanceNow + distanceOverall;
mpgOverall = parseInt(distanceOverall/gasOverall);
document.writeln(
"Your overall mpg is " + mpgOverall + "<br />");
answer = window.prompt(
"If you want to add more values, type yes.")
}
</script>
</head>
<body>
</body>
</html>
是的,这是因为提示出现得太快,DOM 没有时间写,因为有一个待处理的提示请求正在进行。解决方案是在您的提示方法上设置超时。
但是因为你在这里做了一个 while 循环,它会再次做第一个提示而不用等待超时。因此,与其使用 while 循环,不如将其包装在一个函数中,如果最后一个提示为是,则递归调用它。
试试这个 fiddle
(function query() {
gasNow = window.prompt(
"Enter the gallons of gas used: ")
gasNow = parseInt(gasNow);
distanceNow = window.prompt(
"Enter the distance traveled in miles: ")
distanceNow = parseInt(distanceNow);
mpgNow = parseInt(distanceNow / gasNow);
document.writeln(
"Your current mpg is " + mpgNow + "<br />");
gasOverall = gasNow + gasOverall;
distanceOverall = distanceNow + distanceOverall;
mpgOverall = parseInt(distanceOverall/gasOverall);
document.writeln(
"Your overall mpg is " + mpgOverall + "<br />");
setTimeout(() => {
answer = window.prompt(
"If you want to add more values, type yes.")
if(answer === 'yes') query();
}, 1);
})()
在每个 document.write 之后您可以添加一个延迟。这将为呈现文档留出时间。