Vue

VueJS2: Assignment 3

by Aubrey Taylor

HTML

<div id="exercise">
  <h2>VueJS2: Assignment 3</h2>
  <!-- 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>
    <label>Timeout Duration:</label><br>
    <input type="text" v-model="timeout">
    <p>{{ value }}</p>
  </div>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#exercise {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}

Vue

const Message = {
	done: "Done!",
  notYet: "Not yet!",
}
const threshold = 37;

new Vue({
  el: "#exercise",
  data: {
    value: 0,
    threshold: 37,
    id: false,
    timeout: 5000,
  },
    // NOTE: Computed properties cannot also exist in data.
  computed: {
  	result: function() {
    	return this.value > threshold ? Message.done : Message.notYet;
    }
  },
  watch: {
  	result: function(value) {
    	const id = setTimeout(() => {
      	this.value = 0;
      }, this.timeout);
      
      if(this.id) {
      	clearTimeout(this.id);
        this.id = false;
      }
      this.id = id;
    }
  },
  methods: {
  	
  }
})