Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div id="hello">
    <p>
      Ask a yes/no question:
      <input v-model="question">
    </p>
    <p>{{ answer }}</p>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/txios@0.1.0/dist/txios.umd.min.js"></script>
  <script src="https://cdn.bootcss.com/lodash.js/4.17.15/lodash.min.js"></script>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
  </body>
</html>
 
new Vue({
  el: '#hello',
  data () {
    return {
      question: '',
      answer: 'I cannot give you an answer until you ask a question!'
    }
  },
  created () {
    this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
  },
  methods: {
    debouncedGetAnswer () {
      // do nothing
    },
    getAnswer () {
      if (this.question.indexOf('?') === -1) {
        this.answer = 'Questions usually contain a question mark. -)'
        return
      }
      this.answer = 'Thinking...'
      const instance = txios.create()
      instance.interceptors.request.use((config) => {
        config.params = {
          _t: +new Date()
        }
        return config
      })
      instance.get('https://yesno.wtf/api')
        .then((response) => {
        this.answer = _.capitalize(response.data.answer)
      })
        .catch((error) => {
        this.answer = 'Error! Could not reach the API. ' + error
      })
    }
  },
  watch: {
    question: function (newQuestion: string, oldQuestion: string) {
      this.answer = 'Waiting for you to stop typing...'
      this.debouncedGetAnswer()
    }
  }
})
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers