JQuery.toggle () called windowsetInterval () not working

I am trying to alternate two logos every 5 seconds using the following code:

window.setInterval(
    function () {
        //breakpoint 1
        $("#logo").toggle(
            function() {
                //breakpoint 2
                $(this).attr('src', '/Images/logo1.png');
            },
            function() {
                //breakpoint 3
                $(this).attr('src', '/Images/logo2.png');
            }
        );
    },
    5000
);

      

I can get a simple switch to work, but when I put the switch in window.setInterval (), the two handlers won't switch.

I have set breakpoints on the lines right below the comments in the above code. Breakpoint 1 hits every 5 seconds. However, breakpoints 2 and 3 were never hit.

Why are none of the toggle function handlers fired?

+2


a source to share


2 answers


As I know,

toggle()

should be click

ed ....

So,



   $("#logo").toggle(
        function() {
            //breakpoint 2
            $(this).attr('src', '/Images/logo1.png');
        },
        function() {
            //breakpoint 3
            $(this).attr('src', '/Images/logo2.png');
        }
    );

window.setInterval(
    function () {        
        $("#logo").trigger('click');
    },
    5000
);

      

+6


a source


It looks like jQuery is not finding the #logo element.

What does HTML markup look like?

You can try this to see if jQuery finds the #logo element:



//Firebug way
console.log($('#logo').length)

//alert way
alert($('#logo').length)

      

.. Fredrik

0


a source







All Articles