Guest User

Untitled

a guest
Oct 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. var element = document.querySelector('#textinput');
  2.  
  3. var throttledInput = Rx.Observable.fromEvent(element, 'keyup').select( function (ev) {
  4. // Return text value for each key up
  5. return ev.target.value;
  6. }).throttle(
  7. // Throttle input by 500ms
  8. 500
  9. ).where( function (text) {
  10. // Only get input more than 2 characters
  11. return text.length > 2;
  12. }.distinctUntilChanged(
  13. // Returns only distinct text values
  14. );
  15.  
  16. // Combine together with an Ajax call
  17. var serviceCall = throttledInput.select( function (text) {
  18. // Call service wrapped as an Observable sequence
  19. return queryService(text);
  20. }.switchLatest(
  21. // Trim out of order requests to ensure we only have the latest
  22. );
  23.  
  24. // Subscribe to the service call
  25. serviceCall.subscribe( function (results) {
  26. // Handle the results
  27. }, function (err) {
  28. // Handle the errors
  29. });
Add Comment
Please, Sign In to add comment