2

is jquery animated function working on multi thread ?

Here in my code...suppose i click top button and call function working fine....

now click any remaining once....2nd function also work but with 1st call also.

so, how i can call one function at a time?

is it's multi thread thn it's way to kill it?

<table width="1200px" height="600px" align="center">
        <tr>
            <td>
            </td>
            <td style="text-align: center">
                <button id="btntop">
                    top</button>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td style="text-align: right">
                <button id="btnleft">
                    left</button>
            </td>
            <td style="text-align: center">

                <img id="book" src="Image/images.jpg" alt="" width="250" height="123" style="position: relative;
                    left: 10px;" />

            </td>
            <td>
                <button id="btnright">
                    right</button>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td style="text-align: center">
                <button id="btnbottom">
                    bottom</button>
            </td>
            <td>
            </td>
        </tr>
    </table>
    <script type="text/javascript">


        function anim(direction) {


            if (direction == "top") {

                $('#book').animate({

                    top: '-=10'

                }, 200, function () {


                        anim("top");

                });

            }

            if (direction == "left") {
                $('#book').animate({

                    left: '-=10'
                }, 200, function () {

                        anim("left");

                });
            }

            if (direction == "right") {
                $('#book').animate({

                    left: '+=10'
                }, 200, function () {

                        anim("right");

                });
            }

            if (direction == "bottom") {
                $('#book').animate({

                    top: '+=10'
                }, 200, function () {

                        anim("bottom");

                });
            }
        }


        $('#btntop').hover(function () {

            anim("top");

        });


        $('#btnleft').hover(function () {

            anim("left");

        });

        $('#btnright').hover(function () {


            anim("right");

        });

        $('#btnbottom').hover(function () {


            anim("bottom");

        });


    </script>
Kvadiyatar
  • 929
  • 14
  • 29

1 Answers1

1

You are calling your anim() function on hover() which isn't a good idea. Change it to click() instead.

As for the second part add in $('#book').stop(true); before calling anim() in each of your 4 button click handlers:

$('#btntop').click(function () {
    $('#book').stop(true);
    anim("top");
});

$('#btnleft').click(function () {
    $('#book').stop(true);
    anim("left");
});

$('#btnright').click(function () {
    $('#book').stop(true);
    anim("right");
});

$('#btnbottom').click(function () {
    $('#book').stop(true);
    anim("bottom");
});

It will stop all of the currently running animations and start the new one.

boz
  • 4,891
  • 5
  • 41
  • 68
  • No, not as far as I know. Have a look at http://stackoverflow.com/a/1663144/295852 – boz Feb 07 '13 at 12:18