Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于循环onclick按钮 Scimba文本 函数loadDoc(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ 对于(var i=1;i_Javascript_Html_Ajax - Fatal编程技术网

Javascript 用于循环onclick按钮 Scimba文本 函数loadDoc(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ 对于(var i=1;i

Javascript 用于循环onclick按钮 Scimba文本 函数loadDoc(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ 对于(var i=1;i,javascript,html,ajax,Javascript,Html,Ajax,您应该使用计数器 声明一个全局变量(使用var),并在每次循环时增加它。忘记使用循环。循环用于您想要立即执行多次操作的情况 在其核心,这归结为每次单击事件发生时增加一个值,然后根据该新值执行某些操作 首先,将i设置为事件处理程序函数范围之外的变量。实现这一点的简单方法是将i设置为全局变量 (更好的方法是使用to scopei并分配事件处理程序函数,而不是HTML) 然后,在事件处理程序函数中: 增量i 获取您想要的数据 根据i 正如Quentin所说,由于您希望手动增加循环,因此您不能再使用fo

您应该使用计数器


声明一个全局变量(使用var),并在每次循环时增加它。

忘记使用循环。循环用于您想要立即执行多次操作的情况

在其核心,这归结为每次单击事件发生时增加一个值,然后根据该新值执行某些操作

首先,将
i
设置为事件处理程序函数范围之外的变量。实现这一点的简单方法是将
i
设置为全局变量

(更好的方法是使用to scope
i
并分配事件处理程序函数,而不是HTML)

然后,在事件处理程序函数中:

  • 增量
    i
  • 获取您想要的数据
  • 根据
    i

  • 正如Quentin所说,由于您希望手动增加循环,因此您不能再使用for循环。您应该这样做。这将增加每次单击的计数,如果大于限制,则将计数重置为0

    HTML
    正如其他人所说,你不需要一个循环,你需要一些东西来记录按钮被点击的次数

    但是,这很棘手,因为您不知道用户是在响应返回之前还是之后单击按钮。如果他们在响应返回之前单击按钮,则您需要在响应返回时循环,以赶上已经发生的按钮单击次数。如果他们在响应返回之后单击按钮e返回时,您必须保留响应并在用户单击时使用它

    解决这种不确定性的干净方法是使用
    Promise
    s。它们允许您只编写一次逻辑,而不必担心响应是否已经到达

    Scimba文本
    window.loadDoc=(函数(){
    var i=1;
    var xhttp=newXMLHttpRequest();
    open(“GET”,“info1.txt”,true);
    xhttp.send();
    //为结果做出承诺
    var pResponse=新承诺(函数(解析){
    xhttp.onreadystatechange=函数(){
    如果(xhttp.readyState==4&&xhttp.status==200){
    解析(xhttp.responseText);
    }
    };
    });
    返回函数(){
    如果(i<5){
    var c=i;
    //每点击一次,就将承诺链接一次
    预应答。然后(函数(响应){
    document.getElementById(c).innerHTML=响应;
    });
    i+=1;
    }
    };
    })();
    
    你做得很好。唯一需要注意的是结果是异步返回的。你看到错误了吗?只需确保html的其余部分是正确的。我看到你得到的是一个ID为数字的元素。ID应该以字母开头。单击按钮时,你想增加迭代次数吗越来越多还是什么?你的代码看起来很好
    <button type="button" onclick="loadDoc()">Scimba text</button>
    
    <script>
    function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        for (var i = 1; i<5; i++){
          if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById(i).innerHTML = xhttp.responseText;
          }
        }
      };
      xhttp.open("GET", "info1.txt", true);
      xhttp.send();
    }
    </script>
    
    <body>
      <div id="count">0</div>
      <button id="inc-btn">
        Increment
      </button>
    </body>
    
    $(document).ready(function() {
      var count = 0;
      var limit = 5;
      var count_div = $('#count');
      var increment_btn = $('#inc-btn');
    
      increment_btn.click(function() {
        if (++count > limit) {
          count = 0;
        }
    
        count_div.text(count);
      });
    });
    
    <button type="button" onclick="loadDoc()">Scimba text</button>
    
    <script>
    window.loadDoc = (function () {
        var i = 1;
        var xhttp = new XMLHttpRequest();
        xhttp.open("GET", "info1.txt", true);
        xhttp.send();
    
        // create a promise for the result
        var pResponse = new Promise(function (resolve) {
            xhttp.onreadystatechange = function() {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    resolve(xhttp.responseText);
                }
            };
        });
    
        return function () {
            if (i < 5) {
                var c = i;
                // chain off the promise once for each click
                pResponse.then(function (response) {
                    document.getElementById(c).innerHTML = response;
                });
                i += 1;
            }
        };
    })();
    </script>