单击事件 JavaScript 后的终止函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15291229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kill function after click event JavaScript
提问by Cecil Theodore
I have a function that I want to be stopped when I run a 'close' click event.
我有一个功能,我想在运行“关闭”点击事件时停止该功能。
The reasoning behind this is when I run img.start
click event for the second time, the function is called again and is now running twice.
这背后的原因是当我img.start
第二次运行click 事件时,该函数再次被调用并且现在运行了两次。
The function in question is a slideshow, so I really do need the function to only ever be running once on a page.
有问题的功能是幻灯片,所以我确实需要该功能只在页面上运行一次。
So just to be clear, when I click on img.close
, I want the BannerSlideshow
function to stop and not be running anymore.
所以要清楚,当我点击 时img.close
,我希望该BannerSlideshow
功能停止并且不再运行。
Start function click event
启动函数点击事件
$('img.start').click(function () {
BannerSlideshow.Init()
});
End function click event
结束函数点击事件
$('img.close').click(function () {
//stop function BannerSlideshow from running
});
Snippet of slideshow function
幻灯片功能的片段
var BannerSlideshow = (function () {
return {
Init: function () {
//slideshow functionality
}
I have updated my question to be more specific, apologies guys.
我已经更新了我的问题,使其更具体,抱歉各位。
回答by EnterJQ
you can do that by using bind () and unbind ()
你可以通过使用 bind () 和 unbind ()
$('img.close').bind('click',function () {
// your code
$(this).unbind('click');
});
OR
或者
You can use Off()
您可以使用 Off()
$(this).off('click');
回答by Amrendra
use this:
用这个:
event.stopPropagation();
or use on.click
或使用 on.click
and kill using
并杀死使用
$(this).off();
or
或者
use .bind and .unbind();
better see here :http://api.jquery.com/category/events/event-handler-attachment/
最好在这里看到:http: //api.jquery.com/category/events/event-handler-attachment/
回答by Ravi Gadag
回答by mlwacosmos
Why dont you use stop function with a boolean ?
为什么不使用带有布尔值的停止函数?
var running = false;
$('img.close').click(function () {
running = true;
});
});
if running = true, use stop() function otherwise start slide
如果 running = true,则使用 stop() 函数,否则开始幻灯片
I think stop function is better than to unbind handlers
我认为停止功能比解除处理程序更好
回答by Miklos Aubert
What does the StartSlideshow function look like? I guess you could have a global variable to run the slideshow or not...
StartSlideshow 功能是什么样的?我猜你可以有一个全局变量来运行幻灯片或不...
function StartSlideshow() {
globalVariableRunSlideshow = true;
while (globalVariableRunSlideshow) {
/* Do some slideshowing here */
}
}
And then your event could do something like :
然后您的活动可以执行以下操作:
$('img.close').click(function () {
globalVariableRunSlideshow = false;
});