jQuery UI drop event of droppable widget firing twice

孤街醉人 提交于 2019-11-28 10:51:38

That's a known issue using both sortable and droppable on the same element.

You can use the sortable's receive event instead.

jsFiddle Demo

$("#sortable").sortable({
        revert: true,
        receive: function (event, ui) {      
            alert("receive been triggered.");
        }
}).droppable({ });

I had a similar problem. There were 2 droppable elements, one of them had relative position, and another one - absolute. They were not nested, but a part of the one with absolute postion was above the part of the one with relative position (in jQuery docs this is called "intersect"). When I moved a draggable element to the "absolute" droppable, drop event was trigged twice. I have been searching for a few days, but didn't find any solution.

Then I decided to make a simple general workaround. Looks not very attractive, but this will help in any situation with firing 2 drop events.

drop: function( e, ui ) {
    if ( ! $('.already-dropped').length ) {
        $('body').addClass('already-dropped');
        setTimeout( function() {
            $('.already-dropped').removeClass('already-dropped');
        }, 100 );
        // callback code...
    }
}

Maybe I'm a little too late but I posted this hopefully it will help someone. This is the code I use. Basically, if the function is called twice, the interval between them should be quick (quicker than what human usually do) so I set the number to be 200 milliseconds.

I used localStorage to set a variable called "last_call". It's the time the function was called. In the next call, we will check if the function was called previously. If it wasn't called, the function will continue executing. If it was called, we will check when the last time it was called. If it was called within 200 milliseconds (which obviously machine call, not human call), we will not call it again.

    var d = new Date();
    var this_time = d.getTime();
    if ( localStorage.getItem("last_call") != null)
    {
        if ((this_time - parseInt(localStorage.getItem("last_call"))) < 100)
        {

            return;
        }

    }
    localStorage.setItem("last_call", this_time);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!