简体   繁体   English

setTimeout直到我停止页面才开始

[英]setTimeout doesn't start until I stop the page

On the event of a form submit, I have a jquery ajax function that queries for the amount of data uploaded from a file that I can use in making a progress bar. 提交表单时,我有一个jquery ajax函数,该函数查询从文件中上传的数据量,可用于制作进度条。 This is my js file: 这是我的js文件:

var submitted = '';
var counter = 0;

$('#webfileuploadform').submit(function(){

    if(submitted == 'submitted') return false; // prevents multiple submits

    var freq = 1000; // freqency of update in ms
    var progress_url = '/upload_progress/' + $('#id_progress_id').val(); // ajax view serving progress info

    update_progress_bar();

    function update_progress_bar(){
        $.ajax({
            dataType:"json",
            url: progress_url,
            success: function(data){
                    counter ++
                console.log('hit success ' + counter);
            },
            complete: function(jqXHR, status){
                if (status == 'success'){
                    console.log('hit complete', status == success')
                } else {
                    console.lot('hit complete', status == something else')
                }
            }
        });
        setTimeout(function(){update_progress_bar()}, freq);
    }

    submitted = 'submitted'; // marks form as submitted

});

So, the user will use the form to select the file then click submit. 因此,用户将使用表单选择文件,然后单击提交。 This all happens just fine. 这一切都很好。 The form submits, the file is uploaded, and the progress shows just fine when I open my ajax view in a separate tab and keep refreshing it. 当我在单独的选项卡中打开ajax视图并不断刷新时,表单提交,文件上传且进度显示得很好。 What I don't understand is why the update_progress_bar function runs once until I stop the page. 我不明白的是为什么update_progress_bar函数只运行一次,直到我停止页面。 I'll upload a file but until I click the 'x' on my browser I won't get any of the 'hit success' in my console. 我将上传一个文件,但是直到我在浏览器中单击“ x”之前,我的控制台中都无法获得任何“点击成功”。 After I hit stop, the console spits out the 'hit success ' plus the counter. 在我按下停止键后,控制台会吐出“成功命中”和计数器。 But I shouldn't have to hit stop to do this. 但是我不必为此停下来。 I need this to work when the file is uploading. 文件上传时,我需要它来工作。

Any ideas what my problem is? 任何想法我的问题是什么?

** **

EDIT 编辑

** **

Not sure if it makes a difference, but I am doing all this on a django development server. 不知道这是否有所作为,但是我正在django开发服务器上完成所有这些工作。 Which I think shouldn't have a problem with two XMLHTTPRequests in the same session. 我认为在同一会话中使用两个XMLHTTPRequest应该不会有问题。

Try this: 尝试这个:

var $submitted = '';

$('#webfileuploadform').submit(function(){

    if($submitted == 'submitted') return false; // prevents multiple submits

    var freq = 1000; // freqency of update in ms

    update_progress_bar();

    function update_progress_bar(){
        var progress_url = '/upload_progress/' + $('#id_progress_id').val(); // ajax view serving progress info

        $.ajax({
            dataType:"json",
            url: progress_url,
            success: function(data){
                console.log("hit success");
            },
            complete:function(jqXHR, status) {
                if (status == 'success') {
                    setTimeout(update_progress_bar, freq);
                }
            }
        });
    }

    $submitted = 'submitted'; // marks form as submitted

});

As it stands, the code looks good to me. 就目前而言,代码对我来说看起来不错。 I would add some more console logging to make sure the ajax call is returning with the status you expect: 我将添加更多控制台日志记录,以确保ajax调用以您期望的状态返回:

complete:function(jqXHR, status) {
    console.log('got to complete, status: ', status);
    if (status == 'success') {
         console.log('status is success, calling setTimeout.');
         setTimeout(update_progress_bar, freq);
    }
}

In the ajax function I set the async setting to false: 在ajax函数中,我将异步设置设置为false:

$.ajax({
    async: false,
    ...
});

This has solved the problem. 这样就解决了问题。 I have no idea how. 我不知道如何。 From what I understand, this setting forces the ajax request to wait until all other http requests are finished before allowing it to continue. 据我了解,此设置将强制ajax请求等待所有其他http请求完成,然后再继续执行。 With that being the case, I have no idea how this works. 在这种情况下,我不知道它是如何工作的。 I'll keep from answering my own question in case someone knows why this is working this way... 如果有人知道为什么这样工作,我将不回答自己的问题。

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

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