gpt4 book ai didi

javascript - 如何使 javascript 中的 setTimeout 为定义的函数工作?

转载 作者:行者123 更新时间:2023-12-01 16:17:21 25 4
gpt4 key购买 nike

这是我的代码:

$(".btn").click(function() {
var userChosenColor = $(this).attr("id");
animatePress(userChosenColor);
})

function animatePress(currentColor) {
$("." + currentColor).addClass("pressed");
}

setTimeout(animatePress,2000);

因此,当我在我的网站上按下按钮时,应用了“按下”类(使其变暗),但 setTimeout 无法使其恢复为原始颜色。我仔细遵循了功能的布局,所以我不知道为什么它不起作用。提前致谢

编辑:这是我的 HTML 中的 jquery 脚本,可能是因为那个吗?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" charset="utf-8"></script>

最佳答案

这应该有效:

$(".btn").click(function() {
var userChosenColor = $(this).attr("id");
animatePress(userChosenColor);
setTimeout(() => animatePress(userChosenColor),2000);
})

function animatePress(currentColor) {
$("." + currentColor).toggleClass("pressed");
}

关于javascript - 如何使 javascript 中的 setTimeout 为定义的函数工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62550048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com