0

Hi.
I have a continue and stop function.
When i call the stop function by clicking in one of the blue boxes it calls the continue function 1 ms later.

My problem is when i click the 'Document' it should call the stop function again but it does not do that, here is a fiddle for a closer look.

JsFiddle

PS: in the javascript code you have to scroll all the way down becouse i could not include the Jquery plugin.

I marked my problem with this comment where my coding starts:

//############ here starts my part ####################
2
  • 1
    It calls the stop function again, and there is part of your problem. It will bind the same events over and over, and all the event handlers will be called for each binding. When you have clicked 10 times, you will have 1024 click event handlers bound to each element, and the next click will cause another 1024 bindings to be created. Click a few more times, and you will notice that the browser halts for several seconds each time, running thousands of event handlers and binding thousands of more events. Commented Nov 10, 2013 at 17:36
  • First of all thanks for your help. I am new to jquery 1 1/2 weeks or something like that and i wonder how to fix this.becouse for me everthing seems right. Commented Nov 10, 2013 at 18:00

1 Answer 1

1

As indicated in the comments, don't rebind your event each and every time stop() is called. Let's move that binding out, and we're also going to use stopPropgation() so that the click event won't bubble up to the document, which will trigger the plugin to start its mojo again:

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/

Sign up to request clarification or add additional context in comments.

5 Comments

i found a bug in your code but dont know how to fix it. if i click once the contin fucntion will start once: if i click it the second time it will activate the contin fucntion twice and so on. do you know how to fix it ? or someone else?
Here is the jsfiddle how i found it out with an alert link
There is no bug in my code, there is a bug in the code you wrote, ergo, it is YOUR bug. I resolved your issue in your original post, but since you rescinded your acceptance, I'll let you take a whirl with this new issue by yourself. HINT: This problem can be resolved with the answer already supplied.
now i used your code i just added an alert in the continue function. when i first start the scrolling and then click the doc. it works fine but if i click click on of the boxes it stops but when i want to continue it it cals the function continue twice and so on
ok i think i found my bug. but i dont know how to fix it. i am adding another click handler to the document every time my contin function is called

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.