How to stop js setInterval when DOM is removed in Vuejs 3?

I have a component.vue file which houses an SVG. The animations for the SVG are defined inside the tags of the file. The script code modifies the behavior of the different parts of SVG.

The problem is when the DOM is re-rendered (when I go to another router-link/Page). I start getting

Uncaught ReferenceError: <SVG's part id> is not defined

in the console. The animation code has setInterval usage. So there are infinite instances of error – unless I refresh the browser or move back to the page (which has the SVG DOM).

export default {
  name: "JackHammer",

  mounted() {
    var funcs = [

    var daysLeft;
    var frameNumber = 0;
    var endDate = new Date(2021, 1, 20, 10, 0, 0, 0);

      function () {
    function initJackhammer() {
      daysLeft = document.getElementById(funcs[2]);
      window.setInterval(ShowTimeLeft, 1000);

      var jackhammerTip = document.getElementById("jackhammerTip");
      var jackhammer = document.getElementById("jackhammer");
      var jackBody = document.getElementById("jackBody");
      var jackArms = document.getElementById("jackArms");
      setInterval(animateJackhammer, 40);

    function ShowTimeLeft() {
      // if (!document.getElementById("hoursLeft")) {
      //   return;
      // }
      var CurrTime = new Date();
      var timeDiff = (endDate - CurrTime) / 1000;
      var timeDiffInDays = Math.floor(timeDiff / 86400);
      timeDiff -= timeDiffInDays * 86400;
      var TimeDiffHours = Math.floor(timeDiff / 3600) % 24;
      timeDiff -= TimeDiffHours * 3600;
      var TimeDiffMinutes = Math.floor(timeDiff / 60) % 60;
      timeDiff -= TimeDiffMinutes * 60;
      var TimeDiffSeconds = Math.floor(timeDiff % 60);
      daysLeft.textContent = timeDiffInDays;
      hoursLeft.textContent = TimeDiffHours;
      minutesLeft.textContent = TimeDiffMinutes;
      secondsLeft.textContent = TimeDiffSeconds;

    function animateJackhammer() {
      // console.log(jackhammerTip)
      if (!document.getElementById("jackhammerTip")) {

        "translate(0," + (frameNumber % 3) * -3 + ")"

        "translate(0," + -Math.sin(frameNumber * 1.5) + ")"

      var jackBodyAmount = Math.sin(frameNumber) + 2;
        "translate(0," + -jackBodyAmount + ")"
        "translate(0," + -jackBodyAmount + ")"

      if (frameNumber == 1000) frameNumber = 0;

I am new to Vuejs and JS as a whole.
I want to JS code to suspend when the DOM is removed and again start when its rendered again. What can be the most optimized way to achieve this?

Currently, I am just exiting out of the functions if the DOM element does not exist. Would appreciate a better approach.

112 thoughts on “How to stop js setInterval when DOM is removed in Vuejs 3?”

  1. Hello I am so delighted I found your website, I really
    found you by mistake, while I was researching on Google for
    something else, Nonetheless I am here now and would just
    like to say kudos for a fantastic post and a all round exciting blog (I also love the theme/design),
    I don’t have time to look over it all at the minute but I have saved it and also added your RSS feeds, so when I
    have time I will be back to read much more, Please do keep up the excellent job.

  2. I really like your blog.. very nice colors & theme. Did you make
    this website yourself or did you hire someone to do it for you?

    Plz reply as I’m looking to create my own blog and would like to know where u got this from.

  3. I’m impressed, I have to admit. Seldom do I encounter a blog that’s both equally educative
    and engaging, and without a doubt, you’ve hit the nail on the head.

    The problem is something that too few folks are speaking intelligently about.
    I am very happy that I came across this during my hunt for
    something regarding this.

    My website; Kodo Detox Patch

  4. I’ve been surfing on-line greater than three hours nowadays, yet I by no means found any interesting
    article like yours. It is pretty price sufficient for me.
    In my view, if all webmasters and bloggers made just right
    content material as you did, the net will likely be much more
    helpful than ever before.

    My homepage :: Gorges Cream Reviews

  5. You could definitely see your enthusiasm in the
    paintings you write. The arena hopes for more passionate writers such as you who aren’t
    afraid to mention how they believe. At all times
    go after your heart.

    my page … Viking XL

  6. It’s a pity you don’t have a donate button! I’d without a doubt donate to this superb blog!
    I guess for now i’ll settle for book-marking and adding your RSS feed to my
    Google account. I look forward to brand new updates and will talk about this
    blog with my Facebook group. Chat soon!

    Here is my homepage Cognifen Ingredients

  7. I really love your blog.. Excellent colors & theme. Did you create this site
    yourself? Please reply back as I’m looking
    to create my own site and would like to learn where you got this
    from or just what the theme is named. Thanks!

    Here is my page; Dick Fuel

  8. Thanks for your entire work on this site. Gloria loves conducting
    investigations and it’s really easy to understand why.
    My spouse and i know all concerning the dynamic method
    you give very helpful thoughts through this website and as well
    as cause response from other individuals on this article then our favorite simple princess is truly understanding a
    lot. Take advantage of the rest of the new year.
    You are always performing a superb job.

    My web page Sage Elixir

  9. Can I simply say what a relief to discover somebody that genuinely
    knows what they’re talking about on the web.
    You certainly know how to bring a problem to light and make
    it important. More and more people must read this and understand this
    side of the story. It’s surprising you’re not more popular since you certainly have the gift.

    Feel free to surf to my site …

  10. Please let me know if you’re looking for a article writer for your blog.
    You have some really great posts and I think I would be a good
    asset. If you ever want to take some of the load off,
    I’d love to write some content for your blog in exchange for a link back
    to mine. Please blast me an email if interested. Thank you!

  11. I just couldn’t depart your web site prior to suggesting that I really enjoyed the usual information a person provide to your guests?
    Is gonna be back steadily to investigate cross-check new posts

    Look at my blog … xe88 game apk (

  12. I don’t drop a bunch of responses, but i did some searching and wound
    up here How to stop js setInterval when DOM is removed in Vuejs 3?
    – JavaScript Help. And I actually do have a few questions for you
    if it’s allright. Is it just me or does it look like a few of these
    comments look like coming from brain dead folks? 😛 And,
    if you are writing at additional social sites, I would like to keep up with everything fresh you have to post.
    Would you make a list of the complete urls of
    all your community pages like your linkedin profile, Facebook
    page or twitter feed?

    Feel free to visit my web page – Nitro Strive Nitric Oxide

  13. Hi, all the time i used to check blog posts here early in the morning, for the reason that i love to learn more and

    Also visit my homepage :: 3win8 android – Aidan,

  14. Good day! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in exchanging links or maybe
    guest writing a blog article or vice-versa? My blog goes
    over a lot of the same subjects as yours and I think we could greatly
    benefit from each other. If you might be interested feel free to send me an email.
    I look forward to hearing from you! Superb blog by the

    my web site 918kiss plus apk ios download

  15. Please let me know if you’re looking for a author for your site.

    You have some really good articles and I feel I would be a good asset.
    If you ever want to take some of the load off, I’d really like to write some material
    for your blog in exchange for a link back to mine.
    Please shoot me an e-mail if interested. Thanks!

  16. It’s appropriate time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I wish to suggest you few interesting things or advice.
    Perhaps you could write next articles referring to this article.

    I wish to read more things about it!

    Here is my blog post;

  17. Hey there! I understand this is somewhat off-topic but I needed to ask.
    Does managing a well-established blog like yours require a massive amount work?
    I’m brand new to running a blog however I do write in my diary on a daily basis.
    I’d like to start a blog so I can share my personal experience and feelings
    online. Please let me know if you have any kind of suggestions
    or tips for new aspiring bloggers. Appreciate

    Here is my blog;

  18. I’ve been browsing on-line greater than three hours nowadays,
    but I never found any attention-grabbing article like yours.
    It’s lovely worth enough for me. In my opinion, if all website owners and bloggers
    made excellent content as you probably did, the
    web will probably be a lot more helpful than ever before.

    Here is my website: Gorges Cream Review

  19. Greate article. Keep posting such kind of info on your blog.
    Im really impressed by it.[X-N-E-W-L-I-N-S-P-I-N-X]Hey there, You’ve performed a
    great job. I will certainly digg it and for my part recommend to my friends.

    I am confident they will be benefited from this site.

    my webpage;

  20. Hmm it looks like your site ate my first comment (it was super long) so
    I guess I’ll just sum it up what I had written and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring
    blog writer but I’m still new to the whole thing.
    Do you have any helpful hints for novice blog writers? I’d
    certainly appreciate it.

    Also visit my site … Slimingo Reviews

  21. Good day! I could have sworn I?ve visited this site before but after
    browsing through some of the articles I realized it?s new to me.
    Nonetheless, I?m certainly delighted I discovered it and I?ll be bookmarking it
    and checking back often!

    Review my web page Alpha Extracts

  22. Thank you for sharing superb informations. Your web site is very cool.
    I am impressed by the details that you have on this site.

    It reveals how nicely you understand this subject. Bookmarked
    this web page, will come back for more articles. You, my friend, ROCK!
    I found simply the information I already searched all over the place and
    simply could not come across. What a great site.

    My blog KetoRedux

  23. Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that I acquire in fact enjoyed account
    your blog posts. Anyway I will be subscribing to your augment and even I achievement you access consistently quickly.

    My website: Drone Max V2

  24. I am really loving the theme/design of your
    website. Do you ever run into any browser compatibility problems?
    A number of my blog audience have complained about my site
    not working correctly in Explorer but looks great in Safari.
    Do you have any recommendations to help fix this issue?

    my site;


Leave a Comment