javascript - 防止js函数被执行两次

标签 javascript jquery

在我的应用程序中有一个包含国家/地区数据的表格,它也使用分页。对于每一行,都有一个删除按钮,该按钮通过 ajax 触发一个函数,国家从数据库中删除,该行向上滑动,并根据我们所在的页面再次呈现表格。

这是我原来的功能:

$(document).on('click', '.glyphicon-remove', function(event) {
    var thiz = $(this);
    var id = thiz.next('input:hidden').val();

    $.ajax({
        url: '/country/' + id,
        type: 'DELETE'
    })
    .done(function() {
        thiz.parent().parent().parent()
            .find('td')
            .wrapInner('<div style="display: block;" />')
            .parent()
            .find('td > div')
            .slideUp(200, function() {
                thiz.parent().parent().remove();
                    if (curPage) {
                        if ($('.table > tbody > tr').length > 1) {
                            renderCountries(curPage);
                        } else {
                            var page = getURLParameter(curPage, 'page');
                            if (page !== '1') {
                                prevPage = previousPageURL(curPage, 'page');
                                renderCountries(prevPage);
                                curPage = prevPage;
                            } else {
                                renderCountries();
                            }
                        }
                    } else {
                        renderCountries();
                    }
            });
    });
});

但是,我发现slideUp()函数被执行了多次,把整个事情搞砸了,我也不知道是什么原因,所以我改了,现在我我正在使用 delay():

$(document).on('click', '.glyphicon-remove', function(event) {
    var thiz = $(this);
    var id = thiz.next('input:hidden').val();

    $.ajax({
        url: '/country/' + id,
        type: 'DELETE'
    })
    .done(function() {
        thiz.parent().parent().parent()
            .find('td')
            .wrapInner('<div style="display: block;" />')
            .parent()
            .find('td > div')
            .slideUp(200)
            .delay(200, function() {
                thiz.parent().parent().remove();
                    if (curPage) {
                        if ($('.table > tbody > tr').length > 1) {
                            renderCountries(curPage);
                        } else {
                            var page = getURLParameter(curPage, 'page');
                            if (page !== '1') {
                                prevPage = previousPageURL(curPage, 'page');
                                renderCountries(prevPage);
                                curPage = prevPage;
                            } else {
                                renderCountries();
                            }
                        }
                    } else {
                        renderCountries();
                    }
            });
    });
});

但是,现在 delay() 之后执行的函数被执行了两次。我不明白为什么会发生这一切,我想知道如何解决这个问题。

编辑:HTML

<table class="table table-striped table-responsive">
    <thead>
        <tr>
            <th>Name</th>
            <th>Continent</th>
            <th>Capital</th>
            <th>Language</th>
            <th>Population</th>
            <th>Currency</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
            <tr>
                <td>asdfdsaf</td>
                <td></td>
                <td></td>
                <td></td>
                <td>0</td>
                <td></td>
                <td>
                    <form method="POST" action="http://localhost:8000/country/all" accept-charset="UTF-8" class="pull-right"><input name="_token" value="l2LCeqGRZARM6YUR6hqTgoKWNRRxAUcwspf4kf1v" type="hidden">
                        <span class="glyphicon glyphicon-remove btn btn-xs btn-danger"></span>
                        <input name="id" value="1" type="hidden">
                    </form>
                </td>
            </tr>

    </tbody>
</table>

最佳答案

我正在删除一堆额外的代码,但这是我的建议:

$.ajax({
    url: '/country/' + id,
    type: 'DELETE'
})
.done(function() {
    var first = true;
    thiz.parent().parent().parent()
        .find('td')
        .wrapInner('<div style="display: block;" />')
        .parent()
        .find('td > div')
        .slideUp(200)
        .delay(200, function() {
            if (first==true){
                first=false;
                thiz.parent().parent().remove();
                if (curPage) {
                    if ($('.table > tbody > tr').length > 1) {
                        renderCountries(curPage);
                    } else {
                        var page = getURLParameter(curPage, 'page');
                        if (page !== '1') {
                            prevPage = previousPageURL(curPage, 'page');
                            renderCountries(prevPage);
                            curPage = prevPage;
                        } else {
                            renderCountries();
                        }
                    }
                } else {
                    renderCountries();
                }
           }
        });
});

不解决触发两次的问题,只执行一次您的部分代码。

关于javascript - 防止js函数被执行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24996943/

相关文章:

javascript - 在 JavaScript 中,如何在超时时包装 promise ?

JavaScript - 如何从函数传递/存储值以形成隐藏字段?

javascript - firebase 函数将通知推送给特定用户

javascript - 使用 anchor 链接打开自定义 jquery 选项卡

javascript - jQuery 悬停从底部缩放

asp.net - 使用 JQuery 从 View 状态获取值?

php - 为什么这个变量不起作用?查询

javascript - jquery窗口调整大小不起作用

javascript - Mapbox 位于 ('load' 上,function() 显示打开 map 时应隐藏的数据

javascript - 在使用新数据的 jQuery POST 请求后重新呈现 EJS 模板