简体   繁体   English

单击按钮后加载spin.js,这将启动长时间运行的PHP脚本并在同一页面中返回结果

[英]Loading spin.js after button click which starts a long running PHP script and returns results in same page

I am building on my original post: Clicking a button to run casperJS in PHP and echo results in same PHP page with button . 我建立在我的原始文章的基础上: 单击一个按钮以在PHP中运行casperJS并在带有button的同一PHP页面中生成结果 I essentially have a button in an HTML button that when clicked activates a casperjs AJAX, which then returns and prints the script results on the same web page. 我实质上在HTML按钮中有一个按钮,单击该按钮可激活casperjs AJAX,然后返回并在同一网页上打印脚本结果。 I would like to activate the spin.js script to have a loading graphic as the casperjs script runs and eventually returns results (takes about 10-15 seconds). 我想在casperjs脚本运行时激活spin.js脚本以具有加载图形,并最终返回结果(大约需要10-15秒)。 I have read: How to show loading spinner in jQuery? 我读过: 如何显示在jQuery中加载微调器? , but I am wondering if I can do this without jQ; ,但我想知道是否可以不使用jQ来执行此操作; I also read: Loading spin.js with require.js and Show loading gif while waiting for ajax response . 我还阅读了: 用require.js 加载 spin.js在等待ajax响应时显示gif加载

As of right now I can get the spinner to start when I click the button, but when the results return, the spinner does not stop. 到目前为止,单击按钮可以启动微调器,但是当结果返回时,微调器不会停止。 I have this in my HTML: 我的HTML中有这个:

<div class='main'>
      <style>
        .spinner {
          position: fixed;
          top: 50%;
          left: 50%;
        }
      </style>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
      <script src="spin.js"></script>
      <script>
        var opts = {
            lines: 11, // The number of lines to draw
              length: 10, // The length of each line
              width: 5, // The line thickness
              radius: 13, // The radius of the inner circle
              corners: 0, // Corner roundness (0..1)
              rotate: 0, // The rotation offset
              direction: 1, // 1: clockwise, -1: counterclockwise
              color: '#000', // #rgb or #rrggbb or array of colors
              speed: 1, // Rounds per second
              trail: 60, // Afterglow percentage
              shadow: false, // Whether to render a shadow
              hwaccel: false, // Whether to use hardware acceleration
              className: 'spinner', // The CSS class to assign to the spinner
              zIndex: 2e9, // The z-index (defaults to 2000000000)
              top: '50%', // Top position relative to parent
              left: '50%' // Left position relative to parent
            };

        var spinner = null;
        var spinner_div = 0;

        $(document).ready(function() {

          spinner_div = $('#spinner').get(0);

          $("#button_AJAX").on('click', function(e) {
            e.preventDefault();
            if(spinner == null) {
              spinner = new Spinner(opts).spin(spinner_div);
            } else {
              spinner.spin(spinner_div);
            }
          });

        });
      </script>
      <div id='spinner' class='spinner'></div>
    </div>

And this is the ajax script which is activated when the button is clicked: 这是单击按钮时激活的ajax脚本:

// 1: Create the request 
var myRequest;

// feature check!
if (window.XMLHttpRequest) {  // does it exist? we're in Firefox, Safari etc.
    myRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // if not, we're in IE
    myRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

// 2: Create an event handler for our request to call back
myRequest.onreadystatechange = function(){
    if (myRequest.readyState === 4) {
        var p = document.createElement("p");
        var t = document.createTextNode(myRequest.responseText);
        p.appendChild(t);
        document.getElementById("mainContent").appendChild(p);
    }
};

// open and send it
myRequest.open('GET', 'phpscript.php', true);
// any parameters?
myRequest.send(null);

The phpscript.js is what actually starts the casperjs script: phpscript.js实际上是启动casperjs脚本的原因:

<?php
echo exec("/home/user/casperjs/bin/casperjs /full/path/to/your_script.js"); 
?>

If I use this: 如果我使用这个:

var $loading = $('#loadingDiv').hide();
$(document)
  .ajaxStart(function () {
    $loading.show();
  })
  .ajaxStop(function () {
    $loading.hide();
  });

Will I have to place it inside the ajax script that is executed when the button is clicked? 我是否必须将其放置在单击按钮时执行的ajax脚本中? Or in the main HTML page? 还是在HTML主页中? I am not sure how to proceed. 我不确定如何进行。

I was able to start a loading spin gif when I click the AJAX button which executes casperjs in PHP and prints results back in the HTML page. 当我单击AJAX按钮时,我能够启动一个加载旋转gif,该按钮在PHP中执行casperjs并将结果打印回HTML页面。 I used these two sources: jsfiddle and AJAX-Get data from php HTML now looks like this: 我使用了以下两个来源: jsfiddleAJAX- 从php HTML 获取数据现在看起来像这样:

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-2.1.1.min.js" type="text/javascript"></script>
        <title>casperjs Automated Testing Utility</title>
    </head>
    <center>
    <body>
    <div id="mainContent">
<p>Welcome to the casperjs Automated Testing Utility</p>
<br>
  <button id="button_AJAX">Run casperjs AJAX</button>
    </div>
    <br>
    <div id="loading"></div>
    <script type="text/javascript">
    $('#button_AJAX').click(function () {
        // add loading image to div
       $('#loading').html('<img src="loader_image.gif"><br><i>Web harvesting in progress, please wait for results.</i>');
        // run ajax request
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "casperjscript.php",
            success: function (data) {
                setTimeout(function () {
                    $('#loading').html(data.results);
                });
            }
        });
    });
    $("#button_AJAX").click(function() {$("#button_AJAX").text("casperjs executed");});
    </script>
    </div>
</center>
    </body>
</html>

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

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