2

I thought an interval just delayed the function, but as it turns out it actually loops.

When I include some function that stops the interval after the deletor function ends it doesn't trigger that and I still get Test logged to the console.

document.addEventListener("DOMContentLoaded", function(event) {
  let fullURL = window.location.href;
  //let fullURL2 = window.location.host + window.location.pathname;

  if (fullURL === "https://net.adjara.com/" ||
    fullURL === "https://net.adjara.com/Home") {
    var timer = setInterval(deletor, 5);

    function deletor() {
      timer;

      var slider = document.querySelector("#slider-con");
      var bannerTop = document.querySelector("#MainContent > div:nth-child(2)")
      var bannerMiddle = document.querySelector("#MainContent > iframe");
      var bannerRandom = document.querySelector("#MainContent > div:nth-child(3)");

      if (slider) {
        slider.parentNode.removeChild(slider);
      }

      if (bannerTop) {
        bannerTop.parentNode.removeChild(bannerTop);
      }

      if (bannerMiddle) {
        bannerMiddle.parentNode.removeChild(bannerMiddle);
      }

      if (bannerRandom) {
        bannerRandom.parentNode.removeChild(bannerRandom);
      }

      function stopInterval() {
        clearInterval(timer);
      }
      console.log("Test");

      /*if ()
      clearInterval(timer);*/
    };
  } else {
    return false;
  }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
CODER11713
  • 79
  • 6
  • 3
    `setInterval()` repeats infinitely at the given delay. You seem to have confused it with `setTimeout()` which occurs only *once* after the delay passes – Rory McCrossan Oct 03 '19 at 08:10
  • 1
    You can try to use this ```setTimeout(function(){ //do what you need here }, 2000);``` – Synapsis Oct 03 '19 at 08:12
  • 2
    You never call `stopInterval()` which itself calls `clearInterval` which then stops the interval from running. – VLAZ Oct 03 '19 at 08:13
  • I do need an interval, because setTimer doesn't work for some reason. – CODER11713 Oct 03 '19 at 08:17
  • @CODER11713 there is no `setTimer`, the function is `setTimeout`. And it works the same as `setInterval` with the exception that it quits after running once - if `setInterval` executes your function, then `setTimeout` would, too. – VLAZ Oct 03 '19 at 08:19
  • Possible duplicate of [setTimeout or setInterval?](https://stackoverflow.com/questions/729921/settimeout-or-setinterval) – freedomn-m Oct 03 '19 at 08:21

2 Answers2

1

What you're looking for is setTimeout. It runs only once.

setTimeout(deletor, 5);

Also, you don't need to write timer variable inside of your closure like you would in Python. Javascript captures everything that's inside of lexical scope.

user1143094
  • 151
  • 1
  • 5
  • For some reason setTimeout doesn't work. As far as I know, the ads that I delete keep popping up. – CODER11713 Oct 03 '19 at 08:22
  • @CODER11713 then I guess there might be some code that renders new ads constantly. You can try increasing interval like so: setTimeout(deletor, 500); If ads code adds banners on the URL change it might work. You can try increasing it even more. Like 1000 or 2000. But you should consider researching the ads code aswell. – user1143094 Oct 03 '19 at 08:25
0

The code you provided works ¯\_(ツ)_/¯

Maybe the problem is coming from what triggers stopInterval()

But as mentioned in comments / other answers, you might be better off with another method

I wouldn't recommend using setTimeout in your case, because it looks like you are simply waiting for some DOM elements to be loaded. The problem with the timeout is that you can't know for sure how fast is the computer that will run your code. Maybe a bad quality phone with an outdated software that will need way more time to run your code than when you test on your personal computer, and that will not have the elements loaded by the time your function will be executed.

jQuery

For this reason, and since you tagged your question with jQuery I think you could use $(elementYouWaitForTheDOM2beLoaded).ready(function2execute) for each element you are watching instead of a having a loop that waits for the elements to be loaded (documentation for "ready" function)

Vanilla JS

And if you want to do it in pure JS it would be document.querySelector(elementYouWaitForTheDOM2beLoaded).on('load', function2execute))

John
  • 376
  • 2
  • 9
  • The code works, my problem is that I get console.log("Test"); still logged in the console. Even though I specifically stated the following: function stopInterval() { clearInterval(timer); } – CODER11713 Oct 03 '19 at 11:30