javascript - 递归函数&设置超时时间

发布于 2024-12-02 04:03:12 字数 1051 浏览 1 评论 0原文

我正在尝试编写一个 javascript 函数,当被调用时执行函数 DoSomething() 一次, 但可以被触发重复执行该功能,直到触发停止。

我正在使用 setTimeout() 函数。我不确定从性能和内存的角度来看这是否是最好的方法。 如果可能的话我想避免全局变量

<!DOCTYPE html>
<html>
    <script src="jquery.js"></script>

    <script>
    var globalCheckInventory = false;

    $(document).ready(function(){
        // start checking inventory
        globalCheckInventory = true;                 
        myTimerFunction();  
    }); 

    // check inventory at regular intervals, until condition is met in DoSomething
    function myTimerFunction(){
        DoSomething();
        if (globalCheckInventory == true)
        {
            setTimeout(myTimerFunction, 5000);      
        }           
    }

    // when condition is met stop checking inventory
    function DoSomething() {     
        alert("got here 1 ");
        var condition = 1;
        var state = 2 ;
        if (condition == state)
        {
            globalCheckInventory = false;
        }        
    }
    </script>

I am trying to write a javascript function that when called performs function DoSomething() once,
but can be triggered to perform the function repeatedly until triggered to stop.

I am using setTimeout() function. I am not sure if this is best method from performance and memory point of view.
Also I would like to avoid global variable if possible

<!DOCTYPE html>
<html>
    <script src="jquery.js"></script>

    <script>
    var globalCheckInventory = false;

    $(document).ready(function(){
        // start checking inventory
        globalCheckInventory = true;                 
        myTimerFunction();  
    }); 

    // check inventory at regular intervals, until condition is met in DoSomething
    function myTimerFunction(){
        DoSomething();
        if (globalCheckInventory == true)
        {
            setTimeout(myTimerFunction, 5000);      
        }           
    }

    // when condition is met stop checking inventory
    function DoSomething() {     
        alert("got here 1 ");
        var condition = 1;
        var state = 2 ;
        if (condition == state)
        {
            globalCheckInventory = false;
        }        
    }
    </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷爱 2024-12-09 04:03:12

这可能是执行您所描述的操作的更简单的方法:

$(function () {
  var myChecker = setInterval(function () {
    if (breakCondition) {
      clearInterval(myChecker);
    } else {
      doSomething();
    }
  }, 500);
});

This is probably the easier way to do what you're describing:

$(function () {
  var myChecker = setInterval(function () {
    if (breakCondition) {
      clearInterval(myChecker);
    } else {
      doSomething();
    }
  }, 500);
});
我也只是我 2024-12-09 04:03:12

另一种方法是存储计时器 ID 并使用 setIntervalclearInterval

var timer = setInterval(DoSomething);

function DoSomething() {
    if (condition)
        clearInterval(timer);
}

Another way to do it would be the store the timer ID and use setInterval and clearInterval

var timer = setInterval(DoSomething);

function DoSomething() {
    if (condition)
        clearInterval(timer);
}
束缚m 2024-12-09 04:03:12

除了全局命名空间的污染之外,我认为您的实现没有任何问题。您可以使用闭包(自执行函数)来限制变量的范围,如下所示:

(function(){

  var checkInventory = false, inventoryTimer;

  function myTimerFunction() { /* ... */ }

  function doSomething() { /* ... */ }

  $(document).ready(function(){
    checkInventory = true;
    /* save handle to timer so you can cancel or reset the timer if necessary */
    inventoryTimer = setTimeout(myTimerFunction, 5000);
  });

})();

I see nothing wrong with your implementation other than the pollution of the global namespace. You can use a closure (self-executing function) to limit the scope of your variables like this:

(function(){

  var checkInventory = false, inventoryTimer;

  function myTimerFunction() { /* ... */ }

  function doSomething() { /* ... */ }

  $(document).ready(function(){
    checkInventory = true;
    /* save handle to timer so you can cancel or reset the timer if necessary */
    inventoryTimer = setTimeout(myTimerFunction, 5000);
  });

})();
凉宸 2024-12-09 04:03:12

封装它:

function caller(delegate, persist){
    delegate();
    if(persist){
        var timer = setInterval(delegate, 300);
        return {
            kill: function(){
                clearInterval(timer);
            }
        } 
    }   
}
var foo = function(){
    console.log('foo');
}

var _caller = caller(foo, true);
//to stop: _caller.kill()

Encapsulate it:

function caller(delegate, persist){
    delegate();
    if(persist){
        var timer = setInterval(delegate, 300);
        return {
            kill: function(){
                clearInterval(timer);
            }
        } 
    }   
}
var foo = function(){
    console.log('foo');
}

var _caller = caller(foo, true);
//to stop: _caller.kill()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文