Javascript JQuery未在For循环中执行
我是一个jquerynoob,试图编写一个简单的JQuery代码,让文本闪烁三次。我最初的代码如下:Javascript JQuery未在For循环中执行,javascript,jquery,html,Javascript,Jquery,Html,我是一个jquerynoob,试图编写一个简单的JQuery代码,让文本闪烁三次。我最初的代码如下: $("#welcome").click(function () { var i = 1; while (++i < 10) { $("#welcome").fadeOut("slow", function () { $("#welcome").fadeIn("slow"); })();
$("#welcome").click(function () {
var i = 1;
while (++i < 10) {
$("#welcome").fadeOut("slow", function () { $("#welcome").fadeIn("slow"); })();
}
});
$(“#欢迎”)。单击(函数(){
var i=1;
而(++i<10){
$(“#欢迎”).fadeOut(“慢”,函数(){$(“#欢迎”).fadeIn(“慢”)}();
}
});
但由于我可能干预了我无法理解的力量,上面的代码使文本只闪烁了一次。我仔细阅读了闭包,确信下面的代码可以进行更改。不幸的是,事实并非如此
$("#welcome").click(function () {
var i = 1;
while (++i < 10) {
(function (i) {
$("#welcome").fadeOut("slow", function () { $("#welcome").fadeIn("slow"); })();
})(i);
}
});
$(“#欢迎”)。单击(函数(){
var i=1;
而(++i<10){
(职能(一){
$(“#欢迎”).fadeOut(“慢”,函数(){$(“#欢迎”).fadeIn(“慢”)}();
})(i) );
}
});
谁能告诉我这是怎么回事吗?你需要利用动画队列
var $welcome = $("#welcome").click(function () {
var i = 1;
//clear previous animations
$welcome.stop(true, true);
while (++i < 10) {
$welcome.fadeOut("slow").fadeIn("slow");
}
});
var$welcome=$(“#welcome”)。单击(函数(){
var i=1;
//清除以前的动画
$welcome.stop(真的,真的);
而(++i<10){
$welcome.fadeOut(“慢”).fadeIn(“慢”);
}
});
演示:您需要使用动画队列
var $welcome = $("#welcome").click(function () {
var i = 1;
//clear previous animations
$welcome.stop(true, true);
while (++i < 10) {
$welcome.fadeOut("slow").fadeIn("slow");
}
});
var$welcome=$(“#welcome”)。单击(函数(){
var i=1;
//清除以前的动画
$welcome.stop(真的,真的);
而(++i<10){
$welcome.fadeOut(“慢”).fadeIn(“慢”);
}
});
演示:您需要使用动画队列
var $welcome = $("#welcome").click(function () {
var i = 1;
//clear previous animations
$welcome.stop(true, true);
while (++i < 10) {
$welcome.fadeOut("slow").fadeIn("slow");
}
});
var$welcome=$(“#welcome”)。单击(函数(){
var i=1;
//清除以前的动画
$welcome.stop(真的,真的);
而(++i<10){
$welcome.fadeOut(“慢”).fadeIn(“慢”);
}
});
演示:您需要使用动画队列
var $welcome = $("#welcome").click(function () {
var i = 1;
//clear previous animations
$welcome.stop(true, true);
while (++i < 10) {
$welcome.fadeOut("slow").fadeIn("slow");
}
});
var$welcome=$(“#welcome”)。单击(函数(){
var i=1;
//清除以前的动画
$welcome.stop(真的,真的);
而(++i<10){
$welcome.fadeOut(“慢”).fadeIn(“慢”);
}
});
演示:这非常有效。演示
$(“#欢迎”)。单击(函数(){
对于(变量x=0;x<3;x+=1){
美元(“#欢迎”)。淡出(“缓慢”);
$(“欢迎”).fadeIn(“慢”);
}
});
另外,如果你知道你想做多少次某事。您应该使用
For循环
<代码>While循环适用于您不知道希望它运行多少次的情况。这非常有效。演示
$(“#欢迎”)。单击(函数(){
对于(变量x=0;x<3;x+=1){
美元(“#欢迎”)。淡出(“缓慢”);
$(“欢迎”).fadeIn(“慢”);
}
});
另外,如果你知道你想做多少次某事。您应该使用
For循环
<代码>While循环适用于您不知道希望它运行多少次的情况。这非常有效。演示
$(“#欢迎”)。单击(函数(){
对于(变量x=0;x<3;x+=1){
美元(“#欢迎”)。淡出(“缓慢”);
$(“欢迎”).fadeIn(“慢”);
}
});
另外,如果你知道你想做多少次某事。您应该使用
For循环
<代码>While循环适用于您不知道希望它运行多少次的情况。这非常有效。演示
$(“#欢迎”)。单击(函数(){
对于(变量x=0;x<3;x+=1){
美元(“#欢迎”)。淡出(“缓慢”);
$(“欢迎”).fadeIn(“慢”);
}
});
另外,如果你知道你想做多少次某事。您应该使用For循环
<代码>While循环用于不知道要运行多少次的情况。在队列中设置
$("#welcome").click(function () {
var i = 1;
//clear animations whcih are running at that time
$(this).stop(true, true);
while (++i < 10) {
$(this).fadeOut("slow").fadeIn("slow");
}
});
$(“#欢迎”)。单击(函数(){
var i=1;
//清除当时正在运行的动画
$(this).stop(true,true);
而(++i<10){
$(这个).fadeOut(“慢”).fadeIn(“慢”);
}
});
在队列中设置
$("#welcome").click(function () {
var i = 1;
//clear animations whcih are running at that time
$(this).stop(true, true);
while (++i < 10) {
$(this).fadeOut("slow").fadeIn("slow");
}
});
$(“#欢迎”)。单击(函数(){
var i=1;
//清除当时正在运行的动画
$(this).stop(true,true);
而(++i<10){
$(这个).fadeOut(“慢”).fadeIn(“慢”);
}
});
在队列中设置
$("#welcome").click(function () {
var i = 1;
//clear animations whcih are running at that time
$(this).stop(true, true);
while (++i < 10) {
$(this).fadeOut("slow").fadeIn("slow");
}
});
$(“#欢迎”)。单击(函数(){
var i=1;
//清除当时正在运行的动画
$(this).stop(true,true);
而(++i<10){
$(这个).fadeOut(“慢”).fadeIn(“慢”);
}
});
在队列中设置
$("#welcome").click(function () {
var i = 1;
//clear animations whcih are running at that time
$(this).stop(true, true);
while (++i < 10) {
$(this).fadeOut("slow").fadeIn("slow");
}
});
$(“#欢迎”)。单击(函数(){
var i=1;
//清除当时正在运行的动画
$(this).stop(true,true);
而(++i<10){
$(这个).fadeOut(“慢”).fadeIn(“慢”);
}
});
淡入淡出需要一些时间,您必须等待动画结束后才能运行下一个动画
提供的答案解决了您的问题,因为jQuery足够聪明,可以缓冲您的动画队列,但它可能会给乞丐带来更多的困惑,而且如果您想在渐弱的动画之间做其他事情,您就不能再依赖它了
然后,您必须使用所谓的异步递归方式(woah)编写代码。简单地理解这个代码片段可能会对javascript常规编程有很大帮助
功能闪烁(nbBlinks){
//仅当nbBlinks计数器不为零时闪烁
如果(闪烁>0){
$('welcome').fadeOut('slow',function(){
//在淡出动画之后做一些事情
$(this.fadeIn('slow',function()){
//现在我们完成了迭代,再次闪烁
闪烁(nbBlinks-1);
})
});
}
}
//启动闪烁功能10次
眨眼(10);
淡入淡出需要一些时间,您必须等待动画结束后才能运行下一个动画
提供的答案解决了您的问题,因为jQuery足够聪明,可以缓冲动画队列,但它可能会创建