2

As the title state, I want to avoid a memory leak from my function, but I got quite confused as to how can I achieve this. currently, in my progress, I make an infinite loop in table td but I just realized it, If I using only this, it can add an infinite <td></td> and I want to avoid it, I try to make a Pseudo Code like this achieve what I want:

1. Add An Original Item to a temporary variable
2. Assign it and then destroy the earliest one, so it can only loop in index [1,2,3] where
-- index 1 = is a prev
-- index 2 = is what we see
-- index 3 = is a next
3. every cloned aside from this 3 index

but I got confused as how to achieve this. this is my javascript code to achieve it:

    itemSlider() {
      let autoScroller = document.getElementById("customWrapper");
      let item = autoScroller.getElementsByTagName("td");
      let originalLength = item.length;
      let multiplier = 0;
      let imgScaller = 0;
      let index = 0;
      let temp = [];
      setInterval(function() {
        if (item[imgScaller + 1].classList != undefined) {
          autoScroller.style.transform = `translateX(${-380 * multiplier}px)`;
        }

        if (imgScaller - 1 != -1 && imgScaller != 0) {
          let firstSlide = item[imgScaller - 1];
          let cloneFirst = firstSlide.cloneNode(true);
          autoScroller.appendChild(cloneFirst);
        }

        if (imgScaller) {
          item[imgScaller + 1].classList.add("active");
        }
        if (imgScaller - 1 != -1) {
          item[imgScaller].classList.remove("active");
          item[imgScaller - 1].classList.remove("active");
        }

        multiplier++;
        imgScaller++;

        console.log("Cek before : ", multiplier);
        console.log("Cek original length : ", originalLength);

        if (multiplier % originalLength == 0) {
          // I just wondering maybe I can input the logic after check it with modulo in here
          // But it seems I got confused as how to achieve it
          autoScroller.remove();
        }
      }, 3000);
    }

for you who maybe thinks this is a duplicate, for reference, I already try several topic in here, but still failed to implement it:

  1. how-to-destroy-a-javascript-object

  2. how-to-remove-cloned-element-div-except-first-div-using-jquery

  3. removing-and-cloning-a-dom-element-in-javascript

  4. cloning-removing-input-fields-keeping-element-id-unique

can someone help me to solve this?

Deepak Kumar
  • 1,246
  • 14
  • 38
Rakish Frisky
  • 665
  • 7
  • 23
  • Are you just looking to stop the `setInterval()`? – Daniel_Knights Aug 09 '20 at 15:52
  • not that, I still want for interval to works as long you still stay in this page, it just, the `` tag will clone as long as the interval works, when I see it, let say first I has around `6 of ` but can I just remove the earliest one after t reach around 24 like every 18 it will remove the earliest 6, somethng like that, so when I check in element dev tools, it won't reach until a hundreds of ``, if user open the page for a long time since it will make the component become laggy – Rakish Frisky Aug 09 '20 at 15:55
  • You could use `clearInterval()` once it's reached a certain number of `td`s? – Daniel_Knights Aug 09 '20 at 16:01
  • but if I use `clearInterval()` won't it make my carousel stop? and not do an infinite loop – Rakish Frisky Aug 09 '20 at 16:02
  • Oh I see what you mean. `clearInterval` wouldn't work in that case – Daniel_Knights Aug 09 '20 at 16:07
  • yup, when I try it too it just not the behaviour that i wanted, but I appreciate for the opinion, thank you @Daniel_Knights – Rakish Frisky Aug 09 '20 at 16:14

1 Answers1

2

You could try something like this:

function removeCells() {
  const container = document.querySelector('tr');
  const cells = document.querySelectorAll('td');

  for (let i = 0; i < 6; i++) {
    container.removeChild(cells[i])
  }
}
<table>
  <tbody>
    <tr>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
    </tr>
  </tbody>
</table>

<button onclick="removeCells()">Remove Cells</button>

Just add a condition which checks the count of cells and triggers removeCells() once it reaches a certain number.

EDIT

I've made some changes to your code and I've prevented the infinite loop.

I've changed the setInterval to a recursive setTimeout, moved multiplier and imgScaller to data properties, added the node removal to the start of the function and hard-coded the first three tds as I couldn't find what 7 was referring to:

new Vue({
  el: "#app",
  data() {
    return {
      totalItems: 9,
      currentPosition: 0,
      maxPosition: 0,
      intervalStatus: true,
      multiplier: 0,
      imgScaller: 0
    };
  },

  methods: {
    itemSlider() {
      let autoScroller = document.getElementById("customWrapper");
      let item = autoScroller.getElementsByTagName("td");

      if (item.length === 6) {
        this.multiplier = 0;
        this.imgScaller = 0;

        console.log("Let's Remove it");
        autoScroller.removeChild(autoScroller.children[0])
        autoScroller.removeChild(autoScroller.children[1])
        autoScroller.removeChild(autoScroller.children[2])
      }

      if (item[this.imgScaller].classList !== undefined) {
        this.currentPosition = -1 * item[this.imgScaller].offsetWidth * this.multiplier;
        autoScroller.style.transform = `translateX(${this.currentPosition}px)`;
        autoScroller.style.transition = `0.5s`;
      }

      if (this.imgScaller - 1 !== -1 && this.imgScaller !== 0) {
        let firstSlide = item[this.imgScaller - 1];
        let cloneFirst = firstSlide.cloneNode(true);
        autoScroller.appendChild(cloneFirst);
      }

      if (this.imgScaller) {
        item[this.imgScaller + 1].classList.add("active");
      }
      if (this.imgScaller - 1 !== -1) {
        item[this.imgScaller].classList.remove("active");
        item[this.imgScaller - 1].classList.remove("active");
      }

      this.multiplier++;
      this.imgScaller++;

      console.log("Cek multipler : ", this.multiplier);

      setTimeout(this.itemSlider, 3000);
    },
  },

  mounted() {
    this.itemSlider();
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="merchandise-item-wrapper">
    <table class="merchandise-table">
      <tr id="customWrapper">
        <td class="item-store">
          <div>
            <div class="portfolio-img bg-white position-relative text-center overflow-hidden">
              <img class="merchandise-img" src="https://via.placeholder.com/300x300">
            </div>
            <div style="padding-top: 3vh;">
              <div class="portfolio-hover-main text-center">
                <div class="portfolio-hover-box align-middle">
                  <div class="portfolio-hover-content position-relative">
                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Kode Pesanan</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-code">Putih (TS - {{multiplier}})</p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Deskripsi</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-text number-of-lines-3">
                          Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nostrum, porro! Illo facere soluta molestiae repellat odio porro id est tenetur nesciunt, ea, similique consequuntur? Voluptate dolorum explicabo quo quaerat deserunt?
                        </p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Harga</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-price">Rp 80.000</p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Ukuran</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-size">XS - XXL</p>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </td>
        <td class="item-store">
          <div>
            <div class="portfolio-img bg-white position-relative text-center overflow-hidden">
              <img class="merchandise-img" src="https://via.placeholder.com/300x300">
            </div>
            <div style="padding-top: 3vh;">
              <div class="portfolio-hover-main text-center">
                <div class="portfolio-hover-box align-middle">
                  <div class="portfolio-hover-content position-relative">
                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Kode Pesanan</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-code">Putih (TS - {{multiplier}})</p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Deskripsi</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-text number-of-lines-3">
                          Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nostrum, porro! Illo facere soluta molestiae repellat odio porro id est tenetur nesciunt, ea, similique consequuntur? Voluptate dolorum explicabo quo quaerat deserunt?
                        </p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Harga</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-price">Rp 80.000</p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Ukuran</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-size">XS - XXL</p>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </td>
        <td class="item-store">
          <div>
            <div class="portfolio-img bg-white position-relative text-center overflow-hidden">
              <img class="merchandise-img" src="https://via.placeholder.com/300x300">
            </div>
            <div style="padding-top: 3vh;">
              <div class="portfolio-hover-main text-center">
                <div class="portfolio-hover-box align-middle">
                  <div class="portfolio-hover-content position-relative">
                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Kode Pesanan</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-code">Putih (TS - {{multiplier}})</p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Deskripsi</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-text number-of-lines-3">
                          Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nostrum, porro! Illo facere soluta molestiae repellat odio porro id est tenetur nesciunt, ea, similique consequuntur? Voluptate dolorum explicabo quo quaerat deserunt?
                        </p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Harga</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-price">Rp 80.000</p>
                      </div>
                    </div>

                    <div class="row text-left">
                      <div class="col-10 col-md-11 col-lg-4">
                        <p class="merchandise-category">Ukuran</p>
                      </div>
                      <div class="col-2 col-md-1 col-lg-1">
                        <p class="merchandise-category">:</p>
                      </div>
                      <div class="col-12 col-md-12 col-lg-7">
                        <p class="merchandise-size">XS - XXL</p>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </td>
      </tr>
    </table>
  </div>
</div>
Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49