JavaScript Promise Iterations-退出脚本标签,重新输入,然后删除HTML文件

如何解决JavaScript Promise Iterations-退出脚本标签,重新输入,然后删除HTML文件

我正在编写一个简短的程序,以了解JavaScript中的Promises。

该程序的目标是将一个数字列表(从5减少到0)打印到HTML页面上,每个数字之间的延迟为0.5s。打印0后,将弹出一个确认框,允许用户再次打印相同的数字或停止。

当前,下面的代码大部分可以正常工作,但是在内部循环的第一次和第二次迭代(在countdown()函数中)的行为非常奇怪-在打印第一个{{ 1}}和5

我已经在chrome开发人员中对其进行了很多调试,以尝试找出正在发生的情况,因此,我在程序的下面提供了有关程序实际执行(或似乎正在执行)操作的逐步指南。代码部分,用粗体显示奇怪的行为。

这是大学的一项无记名的工作,我设法挫败了我的教授,因此我们将不胜感激。预先感谢!

4
// wait time for setTimeout
var wait = 500;

function timer(ms) {
  return new Promise((res) => {
    setTimeout(res,ms);
  });
}

async function countdown() {
  for (let i = 5; i >= 0; i--) {
    document.write("<br>");
    document.write(i);
    console.log(i);
    await timer(wait).then();
  }
  var c = confirm("Do you want to go again?");
  document.write("<br>User Confirm:");
  document.write(c);
  console.log(c);
  return c;
}



async function repeatRun() {
  var prom = new Promise(async(res,rej) => {
    r = await countdown();
    if (r) {
      res();
    } else {
      rej();
    }
  });
  prom.then(
    data => {
      document.write("<br>RERUN");
      console.log("RERUN");
      repeatRun();
    },error => {
      document.write("<br>ENDED");
      console.log("ENDED");
    }
  );
}

repeatRun();

这是我的程序正在逐步执行的操作:

  1. 显示HTML页面标题等

  2. 输入<!DOCTYPE html> <html> <head> <title>Countdown</title> </head> <body> <h1>Countdown</h1> <p>Internet Applications Ex1.</p> </body> </html>标签

  3. 输入script

  4. 创建Promise变量repeatRun()并输入prom

  5. countdown()循环的第一次迭代(即for

    5.1。使用i=5打印<br>5,都被附加到HTML正文

    5.2。输入document.write()

    5.3。返回timer()

    5.4。 此承诺返回到new Promise内部的prom.then(),而不是像我期望的那样repeatRun()致电

    此后,无论是否使用await timer(),我都尝试过此方法,它的作用相同[在Chrome调试器上一步完成]

  6. 退出.then()标签

  7. 重新输入script标签,但不是从头开始:它返回到script循环并减小for

    当前HTML文件:

    i
  8. 下一个document.write()调用,不是将<html> <head> <title>Countdown</title> </head> <body> <h1>Countdown</h1> <p>Internet Applications Ex1.</p> <script>...</scri pt> <br> "5" </body> </html> 附加到HTML正文,而是将旧的HTML正文替换为<br><br>标签也被删除

  9. 下一个document.write()将head(现在为4)附加到新的HTML文件中 当前的HTML文件:

    i

这是程序开始按预期运行的地方 10.进入<html> <head></head> <body> <br> "4" </body> 函数,然后返回到timer() 11.按预期循环,直到countdown()

i=0
  1. 此循环后,弹出确认窗口:单击“确定”,将11.1. `document.write()` appends to HTML body instead of rewriting 11.2. `timer()` function is called and returns to `countdown()` function afterwards 存储在true中,使用c打印,然后返回并存储在{{1}中}
  2. document.write()以来,r成功,并且r=true被调用
  3. 输入if,然后将res()prom.then()正确地将data =>附加到HTML正文
  4. document.write函数被递归调用
  5. 创建新的promise var并调用"<br>RERUN"
  6. 这一次,repeatRun()循环如期发生(即在步骤11中),从countdown()for
  7. 循环后,我在确认窗口中单击“取消”,将i=5设置为i=0,进行打印,然后返回并存储在c
  8. 由于falser失败并且r=false被调用
  9. 先输入if,然后再输入rej()和document.write将prom.then()正确地附加到HTML正文
  10. 退出error =>标签

解决方法

MDN documentation for documentation.write()

注意:因为document.write()写入文档流,所以在关闭的(已加载)文档上调用document.write()会自动调用document.open(),这将清除文档。

因此,第一次(第一次迭代)文档以某种方式未完全加载,这意味着它是打开的,因此document.write()照常工作。但是在第一次迭代之后,文档以某种方式完成了加载,因此现在关闭了该文档,现在当它调用document.write()时,由于已关闭,因此必须再次打开该文档,从而清除了整个文档文档(54取代),在其余的迭代中,文档保持打开状态,因此它照常工作。

您不应使用document.write(),而应使用任何其他方法来更改页面的内容。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res