@@ -214,35 +214,34 @@ var watchExampleVM = new Vue({
214
214
// 질문이 변경될 때 마다 이 기능이 실행됩니다.
215
215
question : function (newQuestion ) {
216
216
this .answer = ' 입력을 기다리는 중...'
217
- this .getAnswer ()
217
+ this .debouncedGetAnswer ()
218
218
}
219
219
},
220
- methods : {
220
+ created : function () {
221
221
// _.debounce는 lodash가 제공하는 기능으로
222
222
// 특히 시간이 많이 소요되는 작업을 실행할 수 있는 빈도를 제한합니다.
223
223
// 이 경우, 우리는 yesno.wtf/api 에 액세스 하는 빈도를 제한하고,
224
224
// 사용자가 ajax요청을 하기 전에 타이핑을 완전히 마칠 때까지 기다리길 바랍니다.
225
225
// _.debounce 함수(또는 이와 유사한 _.throttle)에 대한
226
226
// 자세한 내용을 보려면 https://lodash.com/docs#debounce 를 방문하세요.
227
- getAnswer: _ .debounce (
228
- function () {
229
- if (this .question .indexOf (' ?' ) === - 1 ) {
230
- this .answer = ' 질문에는 일반적으로 물음표가 포함 됩니다. ;-)'
231
- return
232
- }
233
- this .answer = ' 생각중...'
234
- var vm = this
235
- axios .get (' https://yesno.wtf/api' )
236
- .then (function (response ) {
237
- vm .answer = _ .capitalize (response .data .answer )
238
- })
239
- .catch (function (error ) {
240
- vm .answer = ' 에러! API 요청에 오류가 있습니다. ' + error
241
- })
242
- },
243
- // 사용자가 입력을 기다리는 시간(밀리세컨드) 입니다.
244
- 500
245
- )
227
+ this .debouncedGetAnswer = _ .debounce (this .getAnswer , 500 )
228
+ },
229
+ methods: {
230
+ getAnswer : function () {
231
+ if (this .question .indexOf (' ?' ) === - 1 ) {
232
+ this .answer = ' 질문에는 일반적으로 물음표가 포함 됩니다. ;-)'
233
+ return
234
+ }
235
+ this .answer = ' 생각중...'
236
+ var vm = this
237
+ axios .get (' https://yesno.wtf/api' )
238
+ .then (function (response ) {
239
+ vm .answer = _ .capitalize (response .data .answer )
240
+ })
241
+ .catch (function (error ) {
242
+ vm .answer = ' 에러! API 요청에 오류가 있습니다. ' + error
243
+ })
244
+ }
246
245
}
247
246
})
248
247
</script >
0 commit comments