Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. (() => {
  2. const test = (result, expect) =>
  3. `${result === expect && '✅' || '🔴'} ${expect}:${result}`
  4.  
  5. const mkRestore = (input, state) => {
  6. return () => {
  7. state.names.pop()
  8. input.value = state.names[state.names.length - 1] || ''
  9. }
  10. }
  11. //-----------------------------------------
  12. const input = {}
  13. const state = {names: ['a', 'b', 'c']}
  14. const restore = mkRestore(input, state)
  15.  
  16. restore()
  17.  
  18. console.log(test(state.names[state.names.length - 1], 'b'))
  19.  
  20. restore()
  21.  
  22. console.log(test(input.value, 'a'))
  23. //-----------------------------------------
  24.  
  25. const debounceInput = (input, delay, state) => {
  26. clearTimeout(state.capture)
  27. state.capture = setTimeout(() => {
  28. state.names.push(input.value)
  29. }, delay)
  30. }
  31.  
  32. //-----------------------------------------
  33. const delay = 500
  34.  
  35. input.value = 'hello'
  36.  
  37. debounceInput(input, delay, state)
  38.  
  39. console.log(test(state.names.length, 1))
  40.  
  41. setTimeout(() => {
  42. console.log(test(state.names.length, 2))
  43. }, delay * 1.5)
  44. //-----------------------------------------
  45. const mkForm = () => {
  46. const form = document.createElement("form")
  47. const { body } = document
  48.  
  49. form.innerHTML = `
  50. <table style="margin: 20px">
  51. <tr>
  52. <td>
  53. <label for="name">name</label>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td>
  58. <input id="name" name="name" value="" />
  59. </td>
  60. </tr>
  61. <tr>
  62. <td>
  63. <button name="restore" style="background-color: red; color; white; padding: 4px">Reset</button>
  64. </td>
  65. </tr>
  66. </table>
  67. `
  68.  
  69. form.onsubmit = () => false
  70.  
  71. body.innerHTML = ''
  72.  
  73. body.appendChild(form)
  74.  
  75. const STATE = { names: [] }
  76.  
  77. const nameInput = document.forms[0].name
  78.  
  79. document.forms[0].restore.onclick = mkRestore(nameInput, STATE)
  80.  
  81. nameInput.onkeyup = () => {
  82. debounceInput(nameInput, 1000, STATE)
  83. }
  84. }
  85.  
  86. mkForm()
  87. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement