简体   繁体   English

jQuery递归函数改为非递归函数

[英]jquery recursive function to non-recursive function

i am sorry this question might be one of those "stupid questions". 对不起,这个问题可能是那些“愚蠢的问题”之一。 I have this round progressbar that works perfectly, when i click the button named "animateButton". 当我单击名为“ animateButton”的按钮时,此圆形进度条运行良好。 But I want to start the progressbar with a function call, but the function does not have a name, because its recursive in the progressbar, i guess. 但是我想通过函数调用来启动进度条,但是该函数没有名称,因为我猜想它在进度条中是递归的。 How do I make the function to start the progress when i call a function like this startIt()?. 当我调用类似startIt()的函数时,如何使函数启动进度?

      $(function() {
      var $topLoader = $("#topLoader").percentageLoader({width: 60, height: 60, controllable : true, progress : 0.0, onProgressUpdate : function(val) {
          $topLoader.setValue(Math.round(val * 100.0));
        }});

      var topLoaderRunning = false;
      $("#animateButton").click(function() {
        if (topLoaderRunning) {
          return;
        }
        topLoaderRunning = true;
        $topLoader.setProgress(0);
        $topLoader.setValue('0kb');
        var kb = 0;
        var totalKb = 500; //100=2.7 1000=27

        var animateFunc = function() {
          kb += 1;
          $topLoader.setProgress(kb / totalKb);
          $topLoader.setValue(kb.toString() + 'kb');

          if (kb < totalKb) {
            setTimeout(animateFunc, 25);
          } else {
            topLoaderRunning = false;
          }
        }

        setTimeout(animateFunc, 25);

      });
    });      

I got it working as easy as this: 我像这样简单地工作:

     startLoader = function() {
    var $topLoader = $("#topLoader").percentageLoader({width: 60, height: 60,    controllable : true, progress : 0.0, onProgressUpdate : function(val) {
  $topLoader.setValue(Math.round(val * 100.0));
      }});

   var topLoaderRunning = false;
if (topLoaderRunning) {
  return;
}
topLoaderRunning = true;
$topLoader.setProgress(0);
$topLoader.setValue('0kb');
var kb = 0;
var totalKb = 500; //100=2.7 1000=27

var animateFunc = function() {
  kb += 1;
  $topLoader.setProgress(kb / totalKb);
  $topLoader.setValue(kb.toString() + 'kb');

  if (kb < totalKb) {
    setTimeout(animateFunc, 25);
  } else {
    topLoaderRunning = false;
  }
}

setTimeout(animateFunc, 25);

   };

If I understand your question correctly, you want something like this: 如果我正确理解了您的问题,则需要以下内容:

$(function() {
  var $topLoader = $("#topLoader").percentageLoader({width: 60, height: 60, controllable : true, progress : 0.0, onProgressUpdate : function(val) {
      $topLoader.setValue(Math.round(val * 100.0));
    }});

  var topLoaderRunning = false;
  var startIt = function() {
    // function body...
  };
  $("#animateButton").click(startIt);
});

You can always assign functions to variables if you need to access them by name. 如果需要按名称访问它们,则始终可以将函数分配给变量。

You already have all the pieces, you just need to reorder them a bit. 您已经拥有了所有部件,只需要对它们重新排序即可。 Give your function a name, then assign that name to the click event. 给您的函数命名,然后将该名称分配给click事件。 Then you can explicitly call the function by name at the end. 然后,您可以在末尾通过名称显式调用该函数。

Untested code below, but I've used this principle many times. 以下未经测试的代码,但是我已经多次使用了这一原理。

$(function() {
  var $topLoader = $("#topLoader").percentageLoader({width: 60, height: 60, controllable : true, progress : 0.0, onProgressUpdate : function(val) {
      $topLoader.setValue(Math.round(val * 100.0));
    }});

  var topLoaderRunning = false;

  var startIt = function() {
    if (topLoaderRunning) {
      return;
    }
    topLoaderRunning = true;
    $topLoader.setProgress(0);
    $topLoader.setValue('0kb');
    var kb = 0;
    var totalKb = 500; //100=2.7 1000=27

    var animateFunc = function() {
      kb += 1;
      $topLoader.setProgress(kb / totalKb);
      $topLoader.setValue(kb.toString() + 'kb');

      if (kb < totalKb) {
        setTimeout(animateFunc, 25);
      } else {
        topLoaderRunning = false;
      }
    }

    setTimeout(animateFunc, 25);

  };

  $("#animateButton").click(startIt);

  startIt();
});     

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

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