【问题标题】:JQuery ajax succes function run only end of each functionjQuery ajax 成功函数只运行每个函数的结尾
【发布时间】:2020-05-02 13:04:46
【问题描述】:

在我的 jQuery 函数中,每个表格行中的项目是通过以下方法下载的。成功部分中所做的颜色更改命令不是在每一行上工作,而是在所有进程完成后工作。每次下载后我应该怎么做才能让它工作? 我认为这是由于 Thread.Sleep (1000)。但是我下载文件的站点在每个请求之间需要 1 秒。

这是我的 qjuery 函数

        $.each($("tr[class='sec']"), function () {
        if ($(this).children('td:eq(1)').children("input[type='checkbox']").prop('checked')) {
            durum = $(this);
            var beyan = {};
            var secililer = [];
            beyan.Id = $(this).attr("id");
            beyan.TahakkukId = $(this).data("id");
            beyan.KisaKod = $(this).children('td:eq(2)').html();
            beyan.BeyannameTuru = $(this).children('td:eq(4)').html();
            beyan.Ay = $(this).children('td:eq(5)').html().substring(8, 10);
            beyan.Yil = $(this).children('td:eq(5)').html().substring(11, 16);
            secililer.push(beyan);
            $.ajax({
                url: '/Ebeyan/BelgeAl',
                type: "POST",
                dataType: "text",
                async: false,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ secililer, islemler, token, }),
                success: function (data) {
                    Indirildi(durum);

                }
            });


        }
    });
function Indirildi(gelen) {
    gelen.css('background-color', 'green');
    gelen.children('td:eq(8)').html("İndirildi");
}

这里是张贴控制器

 public async Task<string> BelgeAl(List<Beyanname> secililer, List<Islem> islemler, string token)
{
    bool indir = true;
    bool yazdir = false;
    bool gonder = false;
    foreach (var islem in islemler)
    {
        if (islem.IslemTuru == "cbyazdir")
        {
            yazdir = true;
        }
        if (islem.IslemTuru == "cbgonder")
        {
            gonder = true;
        }
    }
    foreach (var GelenBeyan in secililer)
    {
        string YolAdi = YolHazirla(GelenBeyan);
        string DosyaAdi = DosyaAdiHazirla(GelenBeyan);

        await dosyaindir(token, YolAdi + "/" + DosyaAdi, "Beyan", GelenBeyan.Id, "");
        await dosyaindir(token, YolAdi + "/" + DosyaAdi, "Tahakkuk", GelenBeyan.Id, GelenBeyan.TahakkukId);

    }
    return "";
}

这是我的下载异步方法

        public async System.Threading.Tasks.Task<Boolean> dosyaindir(string token, string yol, string tur, string id, string thkid)
    {

        string URI = "https://ebeyanname.gib.gov.tr/dispatch?";
        string myParameters = "cmd=IMAJ&subcmd=";
        if (tur == "Beyan")
        {
            myParameters += "BEYANNAMEGORUNTULE";
        }
        else
        {
            myParameters += "TAHAKKUKGORUNTULE&tahakkukOid=" + thkid;
        }
        myParameters += "&TOKEN=" + token;
        myParameters += "&beyannameOid=" + id;
        myParameters += "&inline=true";
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(string.Concat(URI + myParameters));
        Req.CookieContainer = Cc;
        Req.Method = "GET";
        Req.ContentType = "application/pdf;charset=ISO-8859-9";
        HttpWebResponse Res = (HttpWebResponse)Req.GetResponse();
        Req.KeepAlive = false;
        Req.ProtocolVersion = HttpVersion.Version11;
        Req.ServicePoint.ConnectionLimit = 1;
        Stream dosya = Res.GetResponseStream();
        yol += tur + ".pdf";
        using (var fs = System.IO.File.Create(yol))
        {
            dosya.CopyTo(fs);
        }
        Res.Close();
        Req.Abort();
        Thread.Sleep(1000);     
        return true;
    }

更新 我刚刚尝试删除

Thread.Sleep(1000); 

并使用此功能,但没有任何改变

 $.each($("tr[class='sec']"), function () {
        if ($(this).children('td:eq(1)').children("input[type='checkbox']").prop('checked')) {
            durum = $(this);
            var beyan = {};
            var secililer = [];

            setTimeout(function () {

            }, 1000);




            beyan.Id = $(this).attr("id");
            beyan.TahakkukId = $(this).data("id");
            beyan.KisaKod = $(this).children('td:eq(2)').html();
            beyan.BeyannameTuru = $(this).children('td:eq(4)').html();
            beyan.Ay = $(this).children('td:eq(5)').html().substring(8, 10);
            beyan.Yil = $(this).children('td:eq(5)').html().substring(11, 16);
            secililer.push(beyan);
            $.ajax({
                url: '/Ebeyan/BelgeAl',
                type: "POST",
                dataType: "text",
                async: false,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ secililer, islemler, token, }),
                success: function (data) {
                   durum.css('background-color', 'green');
                   durum.children('td:eq(8)').html("İndirildi");

                }
            });


        }
    });

【问题讨论】:

  • 问题是你的js里的gelen是什么?那应该是当前行吗?
  • 是的,抱歉,我尝试使用在过去 function Indirildi(gelen) { gelen.css('background-color', 'green'); gelen.children('td:eq(8)').html("İndirildi"); } 之前忘记更改的函数,成功函数是 Indirildi(durum);
  • 我可以在每个函数中等待这个过程并删除Thread.Sleep(1000);
  • 是的,您可以使用 setTimeout(function() {} ,500) 其中 500 是毫秒数
  • 但我不确定这一定会解决它

标签: c# jquery


【解决方案1】:

我在here 中找到了我的解决方案 我删除每个功能, 并将代码放入带有 SetTimeOut 的函数中

 (function IslemYap() {
        i++;
        satir = $("tr[class='sec']:eq(" + i + ")");
        if (satir.children('td:eq(1)').children("input[type='checkbox']").prop('checked')) {                
            sayi++;
            var beyan = {};
            var secililer = [];
            beyan.Id = satir.attr("id");
            beyan.TahakkukId = satir.data("id");
            beyan.KisaKod = satir.children('td:eq(2)').html();
            beyan.BeyannameTuru = satir.children('td:eq(4)').html();
            beyan.Ay = satir.children('td:eq(5)').html().substring(8, 10);
            beyan.Yil = satir.children('td:eq(5)').html().substring(11, 16);
            secililer.push(beyan);
            $.ajax({
                url: '/Ebeyan/BelgeAl',
                type: "POST",
                dataType: "text",
                async: false,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ secililer, islemler, token, }),
                success: function (data) {
                    console.log(sayi);
                    satir.css('background-color', 'green');
                    satir.children('td:eq(8)').html(data);
                    $("#pb").width(pbdurum + "%")
                    pbdurum += oran;
                }
            });
        }
        if (i < toplamsayi) {
            setTimeout(IslemYap, 100);
        }

    })();

【讨论】:

    猜你喜欢
    • 2019-04-13
    • 2019-01-20
    • 2015-06-13
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多