简体   繁体   English

引导模式与 Firefox 一起使用,但在 Chrome 中不可用

[英]Bootstrap modal working with Firefox but not in Chrome

This is my code, it closes a modal after pressing a button, opens another one right after and executes a parsing function:这是我的代码,它在按下按钮后关闭一个模式,然后立即打开另一个并执行解析 function:

$('#BtnUpCsv').on('click', function (e) {
      $('#csvModal').modal('hide');
      $('#pBarModal').modal('show');
      Papa.parse(document.getElementById('csvinput').files[0], {...

In Firefox it works as intended but in Chrome the first modal won't close immediatly after the function is called and the parsing starts as it has some weird priority over the first two functions.在 Firefox 中,它按预期工作,但在 Chrome 中,第一个模态不会在调用 function 后立即关闭,并且解析开始,因为它比前两个函数具有一些奇怪的优先级。 After the parse is over the csvModal is closed and the pBarModal appears.解析结束后,关闭csvModal并出现pBarModal I already tried adding a setTimeout but it messes with the parsing function.我已经尝试添加一个setTimeout ,但它与解析 function 混淆。

Any idea why this happens?知道为什么会这样吗?

EDIT: entire function编辑:整个 function

Papa.parse(document.getElementById('csvinput').files[0], {
        header: true,
        skipEmptyLines: true,
        complete: function(results) {
          pBarMax = results.data.length - 1;
          results.data.forEach(function (data) {
            const csvPcode = data.pcode;
            const csvAntiOx = data.AntiOx;
            const csvCalories = data.Calories;
            const csvCarbs = data.Carbs;
            const csvFats = data.Fats;
            const csvFibers = data.Fibers;
            const csvLipides = data.Lipides;
            const csvProteins = data.Proteins;
            const csvSat_fats = data.Sat_fats;
            const csvSodium = data.Sodium;
            const csvSugars = data.Sugars;

            //Update progress bar
            pBarPerc = ((iPbar/PbarMax) * 100).toFixed();
            $('#progressBar').text(+ pBarPerc + '%');
            $('#progressBar').attr('style','width:' + pBarPerc + '%');
            //console.log(pBarPerc);

            //Rpc requests
            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'AntiOx',
                value: csvAntiOx
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Calories',
                value: csvCalories
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Carbs',
                value: csvCarbs
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Fats',
                value: csvFats
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Fibers',
                value: csvFibers
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Lipides',
                value: csvLipides
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Proteins',
                value: csvProteins
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Sat_fats',
                value: csvSat_fats
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Sodium',
                value: csvSodium
              },
              dataType: 'json'
            } );

            $.ajax({
              type: 'POST',
              url: '../contents/cbk-data-process.php',
              async: false,
              data: {
                method: 'prod-variable-saveby-name',
                node: <?php echo $node ?>,
                pcode: csvPcode,
                objectname: 'Sugars',
                value: csvSugars
              },
              dataType: 'json'
            } );
            iPbar++;
          } );
          //Reset progress bar on completition and modal when hidden
          let PbarResult = $('#pBarModalLabel').text('Completed, ' + results.data.length + ' records updated');
          pBarMax = 0;
          pBarPerc = 0;
          $('#tbl-pcodes').DataTable().ajax.reload();
          setTimeout(function() {$('#pBarModal').modal('hide');}, 2000);
          setTimeout(function() {$('#pBarModalLabel').text('Processing... please wait');}, 2500);
        }
      } );
    } );

Ok I think I understand your problem now.好的,我想我现在明白你的问题了。 Here is a JSFiddle I created to recreate the problem, and to show how the solution works: https://jsfiddle.net/rn3y1qgd/7/这是我为重现问题而创建的 JSFiddle,并展示了解决方案的工作原理: https://jsfiddle.net/rn3y1qgd/7/

The reason it seems like your parsing function is taking "priority" over the modals hiding and showing is because the modal functions are asynchronous.您的解析 function 似乎“优先于”隐藏和显示模式的原因是因为模式函数是异步的。 Basically this means when you call them, it won't start updating the html (to hide/show the modals) until after all the code has finished running.基本上这意味着当您调用它们时,它不会开始更新 html(隐藏/显示模式),直到所有代码完成运行。 This means it has to wait for your parsing function to finish before it shows/hides the modals.这意味着它必须等待您的 function 解析完成,然后才能显示/隐藏模态。

The solution is to wait for the modal to be shown before calling the parsing function.解决方法是等待modal显示出来调用解析function。 In the fiddle this is the relevant part:在小提琴中,这是相关的部分:

$("#testbtnSolution").click(function() {
    $("#exampleModal").modal('hide');   
    $("#exampleModal2").one('shown.bs.modal', function() {
      longExecutingFunction(2000);
      alert("processing done");
    }).modal('show');  
});

In this code we register an event handler that runs on shown.bs.modal .在这段代码中,我们注册了一个在shown.bs.modal上运行的事件处理程序。 Once the modal has finished showing, it will then run your processing function.模态显示完成后,它将运行您的处理 function。

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

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