该按钮的目的是使用某种循环弹出一个红色圆圈3秒钟以监视时间

发布于 2025-01-24 19:33:25 字数 789 浏览 1 评论 0原文

我在SetInterval()方面有很少的经验,因此我不确定如何使用它。过去,我什至在此代码中尝试过SetteMeut(),但随后意识到SetInterval()更好。我正在尝试用尽可能小的代码编写它。无论如何,我只需要该代码的这一部分提供帮助,然后我将能够投入其中的大型代码。为了总结最终目的,该代码应该显示隐藏的宝藏在地图上的位置,该圆圈将弹出宝藏所在的位置,用户必须单击字段并尝试找到它。主代码已经完成,但我无法弄清楚如何制作此提示。而setInterval()3000(3秒)圆圈显示时,当圆圈消失时,计时器不断运行,以便用户无法使用提示多于一次。

let elem = document.getElementById("hint");

elem.addEventListener('click', myFunction(){
setInterval(myFunction, 3050);


    function myFunction(time){
        while (time<3000){
        let c = document.getElementById("myCanvas");
        let ctx = c.sdgetContext("2d");
        ctx.beginPath();
        ctx.arc(110,80, 70,0, 2 * Math.PI);
        ctx.fillStyle ="red";
        ctx.globalAlpha = "0.6";
        ctx.fill();
         }
    }
});

I have a small amount of experience with setInterval() so I'm not quite sure how to use it . In the past , I've even tried setTimeout() in this code ,but then realized that setInterval() is better. I'm trying to write it with as little code as I can. Anyways, I only need this part of the code helped out with, and then I'll be able to put in the big one that I' working on . To summarize the ultimate purpose, this code is supposed to show where the hidden treasure is on the map, that circle is gonna pop up where the treasure is located and the user has to click around the field and try to find it. The main code is finished but I just cant figure out how to make this hint. While setInterval() is below 3000(3 seconds) the circle shows, when the circle disappears , the timer keeps going so that the user couldn't use the hint more than once.

let elem = document.getElementById("hint");

elem.addEventListener('click', myFunction(){
setInterval(myFunction, 3050);


    function myFunction(time){
        while (time<3000){
        let c = document.getElementById("myCanvas");
        let ctx = c.sdgetContext("2d");
        ctx.beginPath();
        ctx.arc(110,80, 70,0, 2 * Math.PI);
        ctx.fillStyle ="red";
        ctx.globalAlpha = "0.6";
        ctx.fill();
         }
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文