JQuery单击功能不会调用另一个函数

时间:2013-11-10 17:15:21

标签: jquery function call continue

您好。
我有一个继续和停止功能。
当我通过点击其中一个蓝色框调用停止功能时,它会在1毫秒后调用继续功能。

我的问题是当我点击'文档' 它应该再次调用停止功能,但它不会这样做,这是一个小小的仔细看看。

JsFiddle

PS:在javascript代码中你必须一直向下滚动因为我不能包含Jquery插件。

我在编码开始时用这个评论标记了我的问题:

//############ here starts my part ####################

1 个答案:

答案 0 :(得分:1)

如评论中所示,每次调用stop()时都不要重新绑定您的事件。让我们移动绑定,我们也将使用stopPropgation(),以便click事件不会冒泡到文档,这将触发插件再次启动它的mojo:

function stop(){     
    $("#webticker").trigger('click'); //This method doesn't provide anything, except perhaps making code a bit more readable
}

$("#webticker").click(function(event){
        event.stopPropagation(); //If we don't stop propagation, the click event will bubble up to the document, which will start the ticker again
        $("#webticker").webTicker('stop');

          if($(event.target).is('#img1')) {
            $('#log').html(event.target.id + ' was clicked.');
            timer = setTimeout(contin, 1);
        } else if($(event.target).is('#img2')){
            $('#log').html(event.target.id + ' was clicked.');
            timer = setTimeout(contin, 1);
        } else if($(event.target).is('#img3')){
            $('#log').html(event.target.id + ' was clicked.');
            timer = setTimeout(contin, 1);
        } else if($(event.target).is('#img4')){
            $('#log').html(event.target.id + ' was clicked.');
            timer = setTimeout(contin, 1);
        } else if($(event.target).is('#img5')){
            $('#log').html(event.target.id + ' was clicked.');
            timer = setTimeout(contin, 1);
        }
  });

JSFiddle:http://jsfiddle.net/rLyyR/6/