vue learning: assignment 3

reactive properties

by giordanna

HTML

<script src="https://npmcdn.com/vue/dist/vue.js"></script>

<div id="exercise">
  <!-- 1) Show a "result" of 'not there yet' as long as "value" is not equal to 37 - you can change "value" with the buttons. Print 'done' once you did it -->
  <div>
    <p>Current Value: {{ value }}</p>
    <button @click="value += 5">Add 5</button>
    <button @click="value += 1">Add 1</button>
    <p>{{ result }}</p>
  </div>
  <!-- 2) Watch for changes in the "result" and reset the "value" after 5 seconds (hint: setTimeout(..., 5000) -->
  <div>
    <p>Timeout seconds:</p>
    <input v-model="seconds" type="number" min="1" max="10">
    <p>{{ value }}</p>
  </div>
</div>

JavaScript

new Vue({
  el: '#exercise',
  data: {
    value: 0,
    seconds: 5
  },
  watch: {
    result: function() {
      var that = this;
        setTimeout(function() {
          that.value = 0;
        }, that.seconds * 1000);
    }
  },
  computed: {
    result: function() {
      return this.value >= 37 ? 'done' : 'not there yet';
    }
  }
});