开发者

Automatic API call using HTML textarea data

I have an html form which includes a textarea box and a submit button that calls the API (say the Google Translate API, or Twitter API).

Now I wish to get rid of the submit button and automatically call the API once the user has entered input. I also wish to have a limited number of API calls: ideally just one API call once the user has finished typing in that textarea.

What would be your recommendation on:

Any pointer would be appreciated.


Edit -> About the "finished typing" part: I will never know if the user has finished typing. The only thing I might know is that there is no typing since x seconds. It would be great if I can detect inactivity since 1 or 2 seconds and trigger the API call. Is that possible and how?


PoltoS has given you information about keyUp and setTimeout. Also you can handle onBlur() event. Form is submitted using submit() method of the form. See examples here. BTW you will probably find the sample for all parts of your question there.


You can bind keydown/keyup events of your field to some function that runs setTimeout (with 5 seconds delay) after clearing old one. Once the function mentioned in setTimeout is called - the user has not typed for 5 seconds - execute your API call.

Better would be to show a countdown timer on the right to let user understand that something would happen in 5 or 10 seconds.

And of course it should be done using JavaScript - no additional request to server needed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜