【问题标题】:Stop interval once console.log display undefined停止间隔一次 console.log 显示未定义
【发布时间】:2020-07-27 01:57:59
【问题描述】:

我创建了一个间隔函数来一遍又一遍地运行我的函数,在用新数据刷新 dom 之后,在某个时刻,我的 console.log 将显示未定义。我们如何在我的 console.log 未定义状态时捕获或停止间隔。当 console.log 被未定义的消息触发时,我们可以使用一个函数来捕获吗?

控制台日志

VM64:56         19
VM64:56         18
VM64:56         17
VM64:56         16
VM64:56         15
VM64:56         14
arc_all.js?v=5.1.3.0-1570239956:18     undefined display format making Product Design Risk NaN
arc_all.js?v=5.1.3.0-1570239956:18     undefined display format making <span data-dim="16" data-dimidx="2" class="crosstab-expand">  <i class="fa fa-plus-square-o"></i></span> NaN
arc_all.js?v=5.1.3.0-1570239956:18     undefined display format making Anti-Competitive Conduct Risk NaN

VM64:56         13
...
VM64:56         0

为了进一步阐述我的功能,我创建了这个功能,这个功能。功能工作正常,我不确定我应该采用哪种方法在 console.log 上获取未定义的消息。

 function auto_refresh(start_count) {    
             window.repeat = setInterval(function(){ 
                    start_count = start_count - 1;
                        console.log(start_count)

                const request = $.ajax({ 
                 async : true,
                 success : function(){
                 setTimeout(customStyle(), 100)
                 },
              });

                if (start_count <= 0) {
                //Will clear interval when timer is 0
                  window.clearInterval(window.repeat);
                console.log('Inside the call function to be removed ' + window.repeat);
              }

             }, 1000);
          
            }
          
      //           // Call auto_refresh function and set 1 - 10 , e.g 10 will run interval for 10 seconds and => stop function ,
      //           // Bigger datasets , set to 30  - 150 or which is preferred 
                 auto_refresh(20)

【问题讨论】:

    标签: javascript jquery d3.js


    【解决方案1】:

    您可以检查 window.repeat 值是否未定义,然后执行某些操作。例如,我在未定义时启动了调试器:

    if (window.repeat === undefined) {
      debugger;
    } else {
      console.log('Inside the call function to be removed ' + window.repeat);
    }
    

    【讨论】:

      【解决方案2】:

      setInterval 返回一个值,该值可以传递给clearInterval 以停止间隔。

      【讨论】:

        猜你喜欢
        • 2018-05-28
        • 1970-01-01
        • 2015-06-24
        • 1970-01-01
        • 2019-06-18
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多