English 中文(简体)
设置设置Timout 搞砸了可变javarint 。
原标题:setTimout screws up variable javascript

我遇到了一个脚本的麻烦, 它在隐藏类后删除了对象 X 时间 。

selector = getselector($(this).parent().parent());
console.log("Clicked Cancel");
$(this).parent().parent().addClass( hidden );
setTimeout(function() {
    $(selector).remove();
}, 400);

I edited some piece of script from here to make function getselector since $(this) doesn t work within a setTimeout. now this piece of code works, as long as you don t run it too quickly again. problem seems to be that variable selector gets messed up when a another node gets deleted within the timespan (currently 400ms)

我无法想到一个简单的方法绕过它。 (笑声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声)

最佳回答

答案很简单: 不要让 selector 全球化, 即使用 var 。 哦, 简单地存储元素, 而不是试图构建选择器 :

var elem = $(this).parent().parent();
elem.addClass( hidden );
setTimeout(function() {
    elem.remove();
}, 400);
问题回答

您也可以以下列方式排队删除,这样您的代码就会更辣一点:

$(this).parent().parent().addClass( hidden ).delay(400).queue(function() {
   $(this).remove();
});

在进程开始时设置一个值为真实的变量。在操作中,检查它是否为假,然后将其设置为假,然后一旦完成,返回为真。如果点击太快,它会检查您的变量,再看它是否为真,然后再次执行该动作。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签