Bläddra i källkod

Initialize `_timing` when the user starts typing

shiny-input
Garrick Aden-Buie 6 år sedan
förälder
incheckning
073186bcc9
2 ändrade filer med 38 tillägg och 1 borttagningar
  1. +28
    -0
      inst/shiny-input-app/dev-shiny-input.Rmd
  2. +10
    -1
      inst/shiny-input-app/typing.js

+ 28
- 0
inst/shiny-input-app/dev-shiny-input.Rmd Visa fil

As a convention, As a convention,
the property or method names start with `_`. the property or method names start with `_`.
Let's add a `_timing` property that with initialize with `null`. Let's add a `_timing` property that with initialize with `null`.
(`r github_sha_link("9568fdcd06fa1fe1815dc48bf6efb03f9af68b29")`)


```js ```js
$.extend(typingSpeed, { $.extend(typingSpeed, {
``` ```


</details> </details>

#### Your Turn: Start `_timing`

We're going to use this property to start timing the user's typing.
On the one hand,
when they delete all the text in the text area,
we want to reset the timers.
On the other hand,
when they start typing,
we want to know when they started.

Update the `.getValue()` method so that whenever

- there are no characters in the textarea

`this._timing` is set to `null` and a `null` value is returned to Shiny.

Similarly, when

- `this._timing` is `null`
- and there are any characters in the text area

`this._timing` is updated to the current time (`Date.now()`)
and a `null` value is returned.

Include the timing value in the data returned to Shiny
so that you can verify it's working.

+ 10
- 1
inst/shiny-input-app/typing.js Visa fil

const nchar = el.value.length; const nchar = el.value.length;
const nword = el.value.split(' ').length; const nword = el.value.split(' ').length;


return {nchar, nword};
if (nchar === 0) {
this._timing = null
return null
}
if (!this._timing && nchar > 0) {
this._timing = Date.now()
return null
}

return {nchar, nword, timing: this._timing};
}, },
setValue: function(el, value) { setValue: function(el, value) {
// This method is used for restoring the bookmarked state of your input // This method is used for restoring the bookmarked state of your input

Laddar…
Avbryt
Spara