如何在 HTML 中根据提示显示图像重复计数

问题描述 投票:0回答:1

我想为 2 个单独的图像显示一定数量的重复项。网页会提示用户速度(我已经这样做了),以及他们想要的每个图像的重复次数。

function show_image()
{
    var img = document.createElement("img");
    var img2 = document.createElement("img");
    img.src = "map.png";
    img2.src = "figure-front.png";
    document.body.appendChild(img);
    document.body.appendChild(img2);
    setTimeout("show_image()", speed);
  max = 0;
  max++
  if (max = count)
    {
    return;
      }
}
<!DOCTYPE HTML>

<html lang = "en">

<head>
<meta charset = "UTF-8">
<title>JavaScript Slide Show</title>
<script src = "ShowImages3.js"></script>
<script>
    var speed=prompt("Type how fast you want to duplicate", " ");
    var count=prompt("Type how many image of each you want to duplicate", " ");
</script>
</head>

<body onLoad = "show_image()">

</body>

</html>

但是正如你所看到的,它显然是无限运行的

javascript html image prompt
1个回答
0
投票

您需要移动 setTimeout 并使用

==
===
进行比较。
=
是作业

这是使用 setInterval 的更清晰版本。我还使用 >= 进行测试

let cnt = 0, tId;

const show_image = () => {
  if (cnt >= count) {
    clearInterval(tId);
    return;
  }
  const img = document.createElement("img");
  const img2 = document.createElement("img");
  img.src = "map.png";
  img2.src = "figure-front.png";
  document.body.appendChild(img);
  document.body.appendChild(img2);
  cnt++
};
let speed = prompt("Type how fast you want to duplicate", " ");
let count = prompt("Type how many image of each you want to duplicate", " ");

if (speed && prompt) tId = setInterval(show_image, speed)

© www.soinside.com 2019 - 2024. All rights reserved.