• <tfoot id='qJBh3'></tfoot>
    <i id='qJBh3'><tr id='qJBh3'><dt id='qJBh3'><q id='qJBh3'><span id='qJBh3'><b id='qJBh3'><form id='qJBh3'><ins id='qJBh3'></ins><ul id='qJBh3'></ul><sub id='qJBh3'></sub></form><legend id='qJBh3'></legend><bdo id='qJBh3'><pre id='qJBh3'><center id='qJBh3'></center></pre></bdo></b><th id='qJBh3'></th></span></q></dt></tr></i><div id='qJBh3'><tfoot id='qJBh3'></tfoot><dl id='qJBh3'><fieldset id='qJBh3'></fieldset></dl></div>

        <bdo id='qJBh3'></bdo><ul id='qJBh3'></ul>

        <legend id='qJBh3'><style id='qJBh3'><dir id='qJBh3'><q id='qJBh3'></q></dir></style></legend>
      1. <small id='qJBh3'></small><noframes id='qJBh3'>

        setInterval 内的返回值

        时间:2023-09-04

              <bdo id='WfItW'></bdo><ul id='WfItW'></ul>
            • <i id='WfItW'><tr id='WfItW'><dt id='WfItW'><q id='WfItW'><span id='WfItW'><b id='WfItW'><form id='WfItW'><ins id='WfItW'></ins><ul id='WfItW'></ul><sub id='WfItW'></sub></form><legend id='WfItW'></legend><bdo id='WfItW'><pre id='WfItW'><center id='WfItW'></center></pre></bdo></b><th id='WfItW'></th></span></q></dt></tr></i><div id='WfItW'><tfoot id='WfItW'></tfoot><dl id='WfItW'><fieldset id='WfItW'></fieldset></dl></div>

              <small id='WfItW'></small><noframes id='WfItW'>

                  <tbody id='WfItW'></tbody>
                <tfoot id='WfItW'></tfoot>
                • <legend id='WfItW'><style id='WfItW'><dir id='WfItW'><q id='WfItW'></q></dir></style></legend>

                  本文介绍了setInterval 内的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 setInterval 中返回一个值.我只想执行一些有时间间隔的事情,这就是我尝试过的:

                  I want to return a value inside a setInterval. I just want to execute something with time interval and here's what I've tried:

                  function git(limit) {
                      var i = 0;
                      var git = setInterval(function () {
                          console.log(i);
                          if (i === limit - 1) {
                              clearInterval(git);
                              return 'done';
                          }
                          i++;
                      }, 800);
                  }
                  
                  var x = git(5);
                  console.log(x);
                  

                  而且它不起作用.还有其他方法吗?

                  And it's not working. Is there any other way?

                  我要做的是为特定时间间隔制作动画.然后当我达到极限时(例如 5x 闪烁 $().fadeOut().fadeIn()),我想返回一个值.

                  What I'm going to do with this is to do an animation for specific time interval. Then when i reached the limit (ex. 5x blink by $().fadeOut().fadeIn()), I want to return a value.

                  这是应用程序:

                  function func_a(limit) {
                      var i = 0;
                      var defer = $.Deferred();
                      var x = setInterval(function () {
                          $('#output').append('A Running Function ' + i + '<br />');
                  
                          if (i == limit) {
                              $('#output').append('A Done Function A:' + i + '<br /><br />');
                              clearInterval(x);
                              defer.resolve('B');
                          }
                          i++;
                  
                      }, 500);
                      return defer;
                  }
                  
                  function func_b(limit) {
                      var c = 0;
                      var defer = $.Deferred();
                      var y = setInterval(function () {
                          $('#output').append('B Running Function ' + c + '<br />');
                  
                          if (c == limit) {
                              $('#output').append('B Done Function B:' + c + '<br /><br />');
                              clearInterval(y);
                              defer.resolve('A');
                          }
                          c++;
                  
                      }, 500);
                      return defer;
                  }
                  
                  func_a(3).then( func_b(5) ).then( func_a(2) );
                  

                  这不是很好,它应该打印 A,A,A,Done A,B,B,B,B,B,Done B,A,A,Done A 但在这里被打乱了,似乎延迟 运行所有功能不是一个接一个 而是同时运行.这就是我问这个问题的原因,因为我想在我的 if...

                  This is not functioning well, it should print A,A,A,Done A,B,B,B,B,B,Done B,A,A,Done A but here it is scrambled and seems the defer runs all function not one after the other but simultaneously. That's why I asked this question because I want to return return defer; inside my if...

                  if (i == limit) {
                       $('#output').append('A Done Function A:' + i + '<br /><br />');
                       clearInterval(x);
                       defer.resolve('B');
                       // planning to put return here instead below but this is not working
                       return defer;
                  }
                  

                  推荐答案

                  你希望它等到间隔结束吗?这对运行时来说将是一个真正的痛苦,你会阻止整个页面.现在 JS 中的很多东西都是异步的,所以你必须使用回调、promise 或类似的东西:

                  Do you expect it to wait until the interval ends? That would be a real pain for the runtime, you would block the whole page. Lots of thing in JS are asynchronous these days so you have to use callback, promise or something like that:

                  function git(limit, callback) {
                      var i = 0;
                      var git = setInterval(function () {
                          console.log(i);
                          if (i === limit - 1) {
                              clearInterval(git);
                              callback('done');
                          }
                          i++;
                      }, 800);
                  }
                  
                  git(5, function (x) {
                    console.log(x);
                  });
                  

                  使用 Promise 会如下所示:

                  Using a promise it would look like this:

                  function git(limit, callback) {
                      var i = 0;
                      return new Promise(function (resolve) {
                          var git = setInterval(function () {
                              console.log(i);
                              if (i === limit - 1) {
                                  clearInterval(git);
                                  resolve('done');
                              }
                              i++;
                          }, 800);
                      });
                  }
                  
                  git(5)
                    .then(function (x) {
                      console.log(x);
                  
                      return new Promise(function (resolve) {
                          setTimeout(function () { resolve("hello"); }, 1000);
                      });
                    })
                    .then(function (y) {
                      console.log(y); // "hello" after 1000 milliseconds
                    });
                  

                  添加了用于创建承诺的伪示例

                  Added pseudo-example for promise creation

                  编辑 2:使用两个承诺

                  Edit 2: Using two promises

                  编辑 3:修复 promise.resolve

                  Edit 3: Fix promise.resolve

                  这篇关于setInterval 内的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我必须在javascript函数中返回一些东西吗? 下一篇:JS 构造函数中的返回语句

                  相关文章

                  <small id='lLoZw'></small><noframes id='lLoZw'>

                      • <bdo id='lLoZw'></bdo><ul id='lLoZw'></ul>

                      <i id='lLoZw'><tr id='lLoZw'><dt id='lLoZw'><q id='lLoZw'><span id='lLoZw'><b id='lLoZw'><form id='lLoZw'><ins id='lLoZw'></ins><ul id='lLoZw'></ul><sub id='lLoZw'></sub></form><legend id='lLoZw'></legend><bdo id='lLoZw'><pre id='lLoZw'><center id='lLoZw'></center></pre></bdo></b><th id='lLoZw'></th></span></q></dt></tr></i><div id='lLoZw'><tfoot id='lLoZw'></tfoot><dl id='lLoZw'><fieldset id='lLoZw'></fieldset></dl></div>
                      <legend id='lLoZw'><style id='lLoZw'><dir id='lLoZw'><q id='lLoZw'></q></dir></style></legend>
                    1. <tfoot id='lLoZw'></tfoot>