最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Kill function after click event JavaScript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

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.

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.

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.

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.

Share Improve this question edited Mar 8, 2013 at 10:12 Cecil Theodore asked Mar 8, 2013 at 9:57 Cecil TheodoreCecil Theodore 9,95910 gold badges32 silver badges38 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

you can do that by using bind () and unbind ()

$('img.close').bind('click',function () {
         // your code
    $(this).unbind('click');
});

OR

You can use Off()

$(this).off('click');

use this:

event.stopPropagation(); 

or use on.click

and kill using

$(this).off();

or

use .bind and  .unbind();

better see here :http://api.jquery./category/events/event-handler-attachment/

you can use off method. Off()

(As of jQuery 1.7, the .on() and .off() methods are preferred to attach and remove event handlers on elements

$('img.close').off('click');

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

I think stop function is better than to unbind handlers

What does the StartSlideshow function look like? I guess you could have a global variable to run the slideshow or not...

function StartSlideshow() {
    globalVariableRunSlideshow = true;
    while (globalVariableRunSlideshow) {
         /* Do some slideshowing here */
    }
}

And then your event could do something like :

$('img.close').click(function () {
    globalVariableRunSlideshow = false;
});
发布评论

评论列表(0)

  1. 暂无评论