内存泄漏与setInterval

时间:2017-07-13 10:48:02

标签: javascript php memory-leaks

我目前遇到的问题是使用JS SetInterval自定义PHP网站上的内存泄漏

我的网站每隔30秒从外部PHP文件加载值,并将它们附加到HTML对象,在这种情况下为<p>标记。

<script>
$(window).on('load', function() {

setInterval (function foo1(){

     $.get("scriptref.php", function( data ) { //receiving the echo from the php data file
          $("#ref").html(data); //putting the echoed value directly into the p tag
       });

    return foo1;
}(), 30000)});
    </script>

我还有一个倒计时器,可以在几秒钟内向下计数到指定值。

<script>

                    var timer = document.getElementById("demo");

                    var snd = new Audio("/assets/audio/horn.wav"); // buffers automatically when created



                    // Update the count down every 1 second
                    var x = setInterval(function() {

                        var jsnextbreach = document.querySelector("#test").innerHTML;



                        // Set the date we're counting down to
                        var countDownDate = new Date(jsnextbreach).getTime();

                        // Get todays date and time
                        var now = new Date().getTime();

                        // Find the distance between now an the count down date
                        var distance = countDownDate - now;

                        // Time calculations for days, hours, minutes and seconds
                        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
                        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

                        // Output the result in an element with id="demo"
                        document.getElementById("demo").innerHTML = days + "d " + hours + "h "
                        + minutes + "m " + seconds + "s ";

                        // If the count down is over, write some text 
                        if (distance < 0) {

                            document.getElementById("demo").innerHTML = "BREACHED";
                        }

                        // If the count down is less than an hour replace demo class to warning. 
                        if (distance < 3600000 ) { 
                        timer.className = "warning" ;

                        }

                        if (distance > 3600001 ) { 
                        timer.className = "normal" ;

                        }

                        if (distance < 3600000 && distance > 3598000) { 

                        snd.play();

                        }


                        },1000);






                    </script>

我的问题是,在页面显示一个小时后,我得到“Aw snap,谷歌浏览器在尝试显示此网页时内存不足”错误。查看分析器,我可以看到内存使用量增长缓慢,但我不知道从哪里开始寻找导致此泄漏的原因。

如果有人能够告知我的代码可能出错的任何内容,或者我如何开始排查导致泄漏的具体因素,我将非常感激!

0 个答案:

没有答案