简体   繁体   English

由于ajax jquery函数,clearTimeout无法正常工作,引导模式事件未处理

[英]clearTimeout doesn't work due to ajax jquery function, bootstrap modal event not handled

I've a ridiculous problem with my javascript setTimeout and jquery ajax function. 我的javascript setTimeoutjquery ajax函数存在一个荒谬的问题。

I've a webpage who needed to be refreshed every x seconds. 我有一个网页,需要每x秒刷新一次。

I use a setTimeout who call my ajax function every x seconds. 我使用setTimeout谁每x秒调用一次我的ajax函数。

The user has the opportunity to use a boostrap modal to enter information. 用户有机会使用boostrap模式输入信息。 What I want is to clear the timeout when the modal is shown and restart the timeout when the user closed. 我想要的是在显示模式时清除超时,并在用户关闭时重新启动超时。

My problem is on the event “shown.bs.modal” none of the functions are executed, even the alerts so my setTimout is still running while the modal is open. 我的问题是在事件“ shown.bs.modal”上,没有任何功能被执行,甚至没有警报,因此在模式打开时,我的setTimout仍在运行。

If the DOM is uploaded while the modal is shown, the modal source code will be deleted and so I'll have a frozen webpage, without scrollbar. 如果在显示模态时上传了DOM,则模态源代码将被删除,因此我将拥有一个冻结的网页,没有滚动条。

The problem comes from the ajax function . 问题来自ajax函数 If I change the code of doRefresh to just an alert(); 如果我将doRefresh的代码更改为alert(); everything works perfectly. 一切正常。

//refresh delay in ms
var delayRefresh=5000;

// stored setTimeout's id
var refresh;

//First call of the ajax function
$(document).ready(function(){
    refresh=window.setTimeout(function(){doRefresh();}, delayRefresh);
});

//code executed when the user open a modal
$('.modal').on('shown.bs.modal', function () {          
     alert(refresh);
    //Stopped the refresh
    clearTimeout(refresh);  
});

//code executed when the user close the modal
$('.modal').on('hidden.bs.modal', function () { 
    alert(refresh);
    //restart of the refresh
    refresh=window.setTimeout(function(){doRefresh();}, delayRefresh);  
    alert(refresh);
});


/* Fonction that run the ajax request to the server */  
var doRefresh = function (){        
    $.ajax({
        type:"PUT",
        url:"<c:url value="/checklist"/>",
        contentType: false,
        async: true,
        processData: false,
        success:function(data){
                // DOM update
                $("#content_needed_to_be_updated").html(data) ;
                //restart of the refresh
                refresh=window.setTimeout(function(){doRefresh();},delayRefresh);

            },
            error:function(xhr){                    
                toastr.error('Le serveur n\'a pas pu être atteint.', 'Erreur !');        
                //restart of the refresh
                refresh=window.setTimeout(function(){doRefresh();}, delayRefresh+20000);
            }
        });             
}; 

New version: 新版本:

//refresh delay in ms
var delayRefresh=30000;

// stored setTimeout's id
var idSetTimeout;

var refesh=function(){
    idSetTimeout=window.setTimeout(function(){doRefresh();}, delayRefresh);
}; 

//First call of the ajax function
$(document).ready(function(){
    refesh();
});

//code executed when the user open a modal
$('.modal').on('shown.bs.modal', function () {          
     alert(idSetTimeout);
    //Stopped the refresh
    clearTimeout(idSetTimeout); 
});

//code executed when the user close the modal
$('.modal').on('hidden.bs.modal', function () { 
    alert(idSetTimeout);
    //restart of the refresh
    refresh();  
    alert(idSetTimeout);
});


/* Fonction that run the ajax request to the server */  
var doRefresh = function (){        
    $.ajax({
        type:"PUT",
        url:"<c:url value="/checklist"/>",
        contentType: false,
        async: false,
        processData: false,
        success:function(data){
                // DOM update
                $("#content_needed_to_be_updated").html(data) ;
                //restart of the refresh
                refresh();                                      
            },
            error:function(xhr){                    
                toastr.error('Le serveur n\'a pas pu être atteint.', 'Erreur !');        
                //restart of the refresh
                refresh();  
            }
        });             
}; 

You could leave your timeout set, and simply check whether the modal is shown or not before making the ajax call to the server. 您可以保留超时设置,仅在对服务器进行ajax调用之前检查是否显示了模式。

var doRefresh = function () {
    if (!$(".modal").data('bs.modal').isShown)
    {
        $.ajax({
            // your code...
        });
    }
}

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

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