我想在我放弃后立即禁用拖动500毫秒

时间:2012-11-30 22:49:09

标签: jquery jquery-ui jquery-ui-sortable

因此,当我重新排序列表时,只要我删除该项目,它就会将位置提交到我的后端代码,并在后端重新排序列表。但是,我遇到了一个案例,如果我真的很快重新排序,那么前端和后端的列表会在一段时间后不再同步。所以我想在我放下一小段时间后立即禁用拖动以避免竞争状态。

function reorder(panelId, type){
    $(escapeClientId(panelId)).sortable({
        start: function(event, ui){
            ui.item.startPos = ui.item.index();
        },
        stop: function(event, ui){
            //submit my start and end position to the server
            //change the cursor to wait
            $('body').css('cursor', 'wait');
            window.setTimeout(wait,600);                                    
        }
    });
    $(escapeClientId(panelId)).disableSelection();
}
function wait(){
    $('body').css('cursor', 'default');
}

如您所见,我将光标更改为wait光标600毫秒,但即使光标处于等待模式,它仍然允许用户拖动以重新排序。我放弃后,有没有办法在短时间内完全禁用拖动?

1 个答案:

答案 0 :(得分:2)

您可以尝试使用disableenable方法:

function reorder(panelId, type) {
    $(escapeClientId(panelId)).sortable({
        start: function (event, ui) {
            ui.item.startPos = ui.item.index();
        },
        stop: function (event, ui) {
            var panel = $(this), 
                body = $('body');

            panel.sortable("disable"); // Disable before submitting.
            body.css('cursor', 'wait');

            //submit my start and end position to the server
            //change the cursor to wait

            setTimeout(function () {
                body.css('cursor', 'default');
                panel.sortable("enable");
            }, 500); // Enable after 500 ms.
        }
    });
    $(escapeClientId(panelId)).disableSelection();
}