weixin_33736832 2018-06-05 17:28 采纳率: 0%
浏览 50


A ajax code that i am using to request a page is consuming too much memory and is making the browser slow and everything lag. It seems like there is recursion going on and i dont know of any way to prevent it. Here is what the code looks like.

$(".item").each(function() {
    $this = $(this);
    var dataString = {s: "<?echo $_SESSION['currentview_'.$stamp]?>", r:"<?echo $search_usernumber?>", st: "<?echo $stamp?>"};
    function timeLeft() {
            type: "POST",
            url: "get_content_home.php",
            dataType: "html",
            data: dataString, 
            success: function(result) {
                window.setInterval(function() {
                }, 500);

How can i solve this problem? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • weixin_33698823 2018-06-05 17:39

    You are recursing and you shouldn't be using this form of nested setInterval. Doing this, will cause an explosion of interval instances. Instead of using setInterval, schedule additional requests using setTimeout.

    setInterval will fire and continue firing every interval until you tell it to stop.

    setTimeout will fire once.

    Let's consider the following code which should address some of the issues you are having in this question as well as your other 2 questions.

    First off, as we said before, don't use setInterval unless you actually want it to run forever. Additionally, don't nest the setInterval creations unless you actually mean to.

    Instead, let's create a recursive function getTimeLeft() that will handle firing the request and scheduling the next check for time left after some duration.

    This example also mocks the $.ajax() function so that you can see the function in action since we don't have an actual back end to use.

    // Fake server side data to keep track of time lefts
    const timeLefts = {
      foo: 0,
      bar: 0,
      fizz: 0,
      buzz: 0
    const timeLeftsUpdateInterval = setInterval(() => {
      for (const [key, val] of Object.entries(timeLefts)) {
        timeLefts[key] = Math.min(val + Math.random() * 10, 100);
      if (Object.entries(timeLefts).every(([k, v]) => v >= 100)) {
    }, 1000);
    // Mock $.ajax function to stub sending AJAX requests
    function $ajax(kwargs) {
      return {
        done: cb => {
          setTimeout(() => {
          }, 500);
    // We will check for an update every second after the last request finishes
    const timeLeftCheckInterval = 1000;
    // Continuously check to see how much time is left for an element
    function getTimeLeft(el) {
      // Make our request data
      const dataString = {
        s: "<?echo $_SESSION['currentview_'.$stamp]?>",
        r: "<?echo $search_usernumber?>",
        st: "<?echo $stamp?>",
        // My custom property to make this work
        x: el.dataset.item
      // Make our request to get the time left
      const req = $ajax({ // Using our mock $.ajax
        type: "POST",
        url: "get_content_home.php",
        dataType: "html",
        data: dataString
      // Once the request has finished
      req.done(data => {
        // Set the time left to the element
        el.innerHTML = data;
        // Have some condition so that you don't check for time left forever
        // Eventually there will be no time left right?  Why keep checking?
        if (data.timeleft <= 0) return;
        // Schedule another round of checking for time left after some duration
        setTimeout(() => {
        }, timeLeftCheckInterval);
    // Kick off getting timeleft for all .items
      .forEach(el => getTimeLeft(el));
      <li class="item" data-item="foo"></li>
      <li class="item" data-item="bar"></li>
      <li class="item" data-item="fizz"></li>
      <li class="item" data-item="buzz"></li>

    This code will address the issue that you are having in 2 Ajax non-blocking because each element will have it's own logic of going and fetching time left and updating itself.

    This also addresses the issue that you are potentially facing in Timer in Ajax - Preemption because now the element won't check to see how much time is left again until after the previous check is finished.




  • ¥20 家用射频美容仪技术规格
  • ¥25 matlab数值溢出该怎么解决?
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)
  • ¥15 在误装Windows server2019 后如何利用Windows.old恢复?
  • ¥20 代码实现状态连接包过滤防火墙的设计与实现