docs icon indicating copy to clipboard operation
docs copied to clipboard

Missing search result for setTimeout

Open michaelCTS opened this issue 1 year ago • 2 comments

I'm new to Vue but have angualarJS (aka angular 1) experience and back then, anything outside of the angular scope had to be brought back into the scope for reactivity. setTimout and XHR calls were notorious for this and could get unwieldy.

Is this not an issue for VueJS anymore? My first instict was to search for setTimeout, but there are no results.

michaelCTS avatar Sep 18 '24 14:09 michaelCTS

@michaelCTS In Vue.js, you don’t need to manually bring things "back into scope" for reactivity, as Vue's reactivity system handles this much more smoothly. Vue uses a reactive data model, automatically tracking changes and propagating to the DOM.

Check this Option API example:

<template>
  <div>{{ message }}</div>
</template>

<script>
export default {
  data() {
    return {
      message: "Hello"
    };
  },
  mounted() {
    setTimeout(() => {
      this.message = "Hello, Vue!";
    }, 2000);
  }
};
</script>

In this example, Vue automatically tracks changes to the message, so when setTimeout updates this.message, the DOM will update as well. There's no need to trigger a scope digest manually.

Note: Since setTimeout is a core function of JavaScript, you can use it directly in Vue components.

mahmudunnabikajal avatar Sep 23 '24 19:09 mahmudunnabikajal

Thanks for explaining @mahmudunnabikajal. I had a look at How reactivity works and understand it better now. Getters, setters and proxies didn't exist back in the days of angularjs / angular 1. Things sure have changed.

Maybe it could be useful to add a sentence to the linked document? Something along the lines of

Thanks to features introduced in ES6, dirty checks due to setTimeout and XHMLHttpRequest`, like in frameworks of old for example AngularJs (v1 of Angular), are no longer necessary.

michaelCTS avatar Sep 24 '24 07:09 michaelCTS