javascript - 在 jQuery 中的each()函数之后进行延迟

标签 javascript jquery

我想在上一个 .dot 延迟 3 秒后为每个 .dot 制作动画 这是我使用的代码。但它不起作用。

var time = 3000;

$(".dot").each(function() {
  setTimeout(function() {
    $(this).addClass("animate");
    $(this).find(".num").addClass("animate");
  }, time)
  time += 3000;
});

最佳答案

this 不引用 setTimeout() 中的元素,因此它不起作用。

您可以将参数传递给函数

var time = 3000;
$(".dot").each(function (i) {
    setTimeout(function (self) {
        self.addClass("animate");
        self.find(".num").addClass("animate");
    }, (i + 1) * time, $(this));
});

关于javascript - 在 jQuery 中的each()函数之后进行延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47328021/

相关文章:

javascript - 在单击事件的按钮位置添加微调器

javascript - METEOR - 将数据从父模板传递到子模板

php - 使用 jQuery 但不使用 AJAX 将 JSON 数组传递给 PHP 脚本

javascript - 为什么我在 TypeScript 中收到错误 cannot redeclare block-scoped variable 'name'?

javascript - 确定单击的组件是否具有 routerLink 属性的正确方法是什么?

php - yii dropDownList 使用 Javascript 发布空值动态填充数据

javascript - 如何在不影响背景的情况下对整个内容添加混合效果

jquery - 分页后函数不会执行

javascript - 单击显示 'Div 1' 并隐藏 'Div 2' 并单击单独的按钮显示 'Div 2' 并隐藏 'Div 1'?

javascript - 根据随机数组结果播放特定的声音?