jQuery .each内部的setTimeout()问题 [英] Issues with setTimeout() inside jQuery .each

查看:272
本文介绍了jQuery .each内部的setTimeout()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将无法正常工作.我尝试了不同的变体到处搜寻,但没有运气.

The following code will not work properly. I've tried different variations & searching everywhere but no luck.

i = 1;
var timer = new Array();
jQuery('a').each(function($) {
    i++;
    timer[i] = setTimeout(jQuery(this).remove(), i * 5000)
})

推荐答案

使用函数包装删除元素

i = 1;
var timer = new Array();
jQuery('a').each(function($) {
    i++;
    var thiz = jQuery(this);
    timer[i] = setTimeout(function() { thiz.remove(); }, i * 5000);
})

这篇关于jQuery .each内部的setTimeout()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆