setTimeout 不完全起作用
在以下代码中,我的 moveit()
函数中的 div
正在等待 .click< 中的
setTimeout()
/code> 函数,但 moveit()
中的 p.font-size
动画在单击时立即发生。它不是在等待超时。我确信这是一个基本问题,但这就是我现在所处的水平。
感谢您的任何建议,
<script type="text/javascript">
$(document).ready(function(){
$("#no").click(function() {
$("#sleep").animate({"border-width": "10px"}, "fast");
$("#sleepanswer").animate({ opacity: 0 }, "fast");
$("p:.sleepquestion").replaceWith("That is too bad. Tonight you will sleep better.");
setTimeout(moveit, 2000);
});
$("#yes").click(function() {
$("#sleepanswer").animate({ opacity: 0 }, "fast");
$("p:.sleepquestion").replaceWith("That is great! A good night sleep is important.");
setTimeout(moveit, 2000);
});
});
</script>
<script type="text/javascript">
function moveit() {
$("#sleep").animate({"left": "10px", "width": "150px"}, "slow");
$("p.sleepquestion").animate({"font-size": "16px"}, "slow");
$("#sleepanswer").animate({ "left": "-9999px"}, "fast");
}
</script>
In the following code, the div
animating in my moveit()
function is waiting for the setTimeout()
in the .click
functions, but the p.font-size
animation in moveit()
is happening immediately upon the click. It's not waiting for the timeout. I'm sure it's a basic issue, but that's the level I'm at right now.
Thanks for any suggestions,
<script type="text/javascript">
$(document).ready(function(){
$("#no").click(function() {
$("#sleep").animate({"border-width": "10px"}, "fast");
$("#sleepanswer").animate({ opacity: 0 }, "fast");
$("p:.sleepquestion").replaceWith("That is too bad. Tonight you will sleep better.");
setTimeout(moveit, 2000);
});
$("#yes").click(function() {
$("#sleepanswer").animate({ opacity: 0 }, "fast");
$("p:.sleepquestion").replaceWith("That is great! A good night sleep is important.");
setTimeout(moveit, 2000);
});
});
</script>
<script type="text/javascript">
function moveit() {
$("#sleep").animate({"left": "10px", "width": "150px"}, "slow");
$("p.sleepquestion").animate({"font-size": "16px"}, "slow");
$("#sleepanswer").animate({ "left": "-9999px"}, "fast");
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题可能是您对
.replaceWith()
的使用。它尝试将一个元素替换为另一个元素,但您已尝试将一个元素替换为文本。我认为您只想使用.html()
代替。另外,您不需要使用
setTimeout()
- 您可以使用.delay()
代替。而且,我认为您的选择器p:.sleepquestion
可能不正确。你可以这样做:我还将
.replaceWith()
更改为.html()
并将p:.sleepquestion
更改为p .sleepquestion
。您的函数 moveit 也可以这样编写,将超时放入函数内:
I think the problem may have been your use of
.replaceWith()
. That attempts to replace an element with another, but you've tried to replace an element with text. I think you just want to use.html()
instead.Also, you don't need to use the
setTimeout()
- you can use.delay()
instead. And, I think your selectorsp:.sleepquestion
are probably not right. You can go with this:I also changed
.replaceWith()
to.html()
and changedp:.sleepquestion
top.sleepquestion
.Your function moveit could also be written like this by putting the timeout inside the function:
我遇到了同样的问题,setTimeout() 中的函数调用不起作用。我通过将函数调用放在引号中解决了这个问题。
例如:
I had the same issue with a function call in setTimeout() not working . I solved the issue by surrounding the function call in quotes.
For example: