简体   繁体   English

带有ajax调用的setInterval不会触发成功回调,直到完成另一种表单提交为止

[英]setInterval with ajax call won't fire success callbacks until another form is done submitting

I have an MVC project and I have an asynch action that calls an outside API multiple times in a loop (passing a diff param each time). 我有一个MVC项目,并且有一个异步操作,该操作在一个循环中多次调用外部API(每次都传递一个diff参数)。 I'd like to have another ajax call executed on an interval while waiting for this form to submit so the user can see where they're at. 我想在等待提交此表单的同时,每隔一段时间执行一次ajax调用,以便用户可以看到他们所在的位置。 I have the following setup but all the "success" callbacks from the secondary calls seem to get paused/put into "pending" status until the first form is done. 我具有以下设置,但是来自辅助调用的所有“成功”回调似乎都已暂停/置于“待处理”状态,直到完成第一种形式为止。 Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

My first action that calls the main code to loop through the API calls is set up like this: 我的第一个调用主代码以遍历API调用的动作是这样设置的:

public async Task<ActionResult> Default(

This process could take a few minutes so I wanted to update the user on which params are being passed. 该过程可能需要几分钟,因此我想更新正在传递参数的用户。 I figured when the form submits which calls the Defauly action, I would do a setInterval with an ajax call to another Controller action and return some json to give back to the user, letting them know the value of a static variable that gets updated during the loop. 我计算了提交提交时调用Defauly操作的表单,我将使用ajax调用另一个Controller操作执行setInterval并返回一些json以返回给用户,让他们知道静态变量的值,该变量在环。 So at the top of the controller I add 所以在控制器的顶部,我添加了

private static string mystatus = "started";

Inside the for loop I update this "mystatus". 在for循环中,我更新了此“ mystatus”。 I added another action 我添加了另一个动作

public ActionResult Progress()
{
    dynamic showMessageString = string.Empty;
    showMessageString = new
    {
        param1 = mystatus,
    };
    return Json(showMessageString, JsonRequestBehavior.AllowGet);
}

So I've tried setting the Default action inside both a regular form and Ajax form (Html.Beginform and Html.AjaxForm). 因此,我尝试在常规表单和Ajax表单(Html.Beginform和Html.AjaxForm)中都设置Default操作。 I have a button which submits the form. 我有一个提交表单的按钮。 I've tried both SUBMIT types and BUTTON types, manually submitting with the latter. 我尝试了SUBMIT类型和BUTTON类型,并与后者手动提交。 And when the button is clicked, here is the JS code that handles it while the form is submitting 当单击按钮时,这是在表单提交时处理它的JS代码

function alertMe() {
    alert("start");
    setInterval(function () {getStatus();}, 3000);
}

function getStatus() {
    $.ajax({
        url: '/Home/Progress',
        dataType: 'json',
        type: 'POST',
        success: function (responseText) {
            alert("test1: " + responseText.param2);
        }
    });
    alert("test2");
    }

    $(document).ready(function () {
        $('#submitme').click(function () {
            alertMe();
        });
    });

In theory, this should hit my Progress action every 3 seconds with an ajax call while my long running form is submitting/grinding away. 从理论上讲,当我长时间运行的表单提交/研磨时,这应该每3秒用ajax调用一次我的Progress操作。 I put a breakpoint inside my Progress action. 我在“进度”动作中添加了一个断点。 What actually happens is this alert("test2"); 实际发生的是此alert(“ test2”); gets executed every 3 seconds as expected and in the browser developer tools I can see the "Progress" action getting called by the status shows "pending". 会按预期每3秒执行一次,并且在浏览器开发人员工具中,我可以看到状态显示为“正在执行”的调用“进度”。 Then when the long running form is done submitting, THEN my breakpoint gets hit a bunch of times (once for every 3 seconds of processing) and I get bombarded at the end with a bunch of alert("test1: whatever the final status is") a bunch of times in a row. 然后,当长时间运行的表单完成提交后,我的断点就会被击中很多次(每处理3秒一次),最后我受到一堆警报的轰炸(“ test1:无论最终状态是什么” )连续一堆。

I tried taking the whole Default form out of this and just kept the button that executed the same JS code (calling this Progress action) and it worked just fine, executing the success callback function every 3 seconds. 我尝试从中取出整个Default表单,只是保留了执行相同JS代码的按钮(称为Progress操作),并且效果很好,每3秒执行一次成功回调函数。 it's only when there is another form being submitted that the ajax calls seem to "be put on pause" until the first form is done. 只有在提交另一种表单时,ajax调用才会“暂停”,直到完成第一种表单为止。

Is there a way to make sure these secondary ajax callback functions are executed WHILE the first form is being executed? 有没有一种方法可以确保在执行第一种形式的同时执行这些辅助ajax回调函数?

Bam! 巴姆!

I added this attribute just above my class name and it started working. 我在类名上方添加了此属性,它开始起作用。 [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)] [SessionState的(System.Web.SessionState.SessionStateBehavior.ReadOnly)]

I guess MS defaults to not letting you run different actions simultaneously in case you change session variables and they get messed up. 我猜想MS默认情况下不会让您同时运行不同的操作,以防您更改会话变量而弄乱了它们。 This simple line takes care of it 这条简单的线照顾好它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM