0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

手が止まったらオートセーブする。

Posted at

lodashの_.throttleを使えば簡単に実装できる。

save=_.throttle(_save,10*1000)

サーバーの通信制限をフロント側で回避できる。

例えば、gistへの認証付きcorsなら10秒に1回程度のリクエストは規制にかからない。

_save=(str)=>{
  //to gist, with api
}

oninput系の間引きにも使える。

t.oninput =_.debounce(on,50); //fps20 =1000/20

全部

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
let fn={}
fn.ce=(d=>document.createElement(d))
fn.now =function(time){
 /*add local time jp*/	 
 if(time=='jp'||time=='jpn') return new Date( Date.now()+ 1000*60*60*9  ).toISOString().split('.')[0] +'Z'
 if(time) return new Date(time).toISOString().split('.')[0] +'Z';
 else return new Date( Date.now() ).toISOString().split('.')[0] +'Z';
}
;
let t=fn.ce('textarea')
,o=fn.ce('pre')
,l=fn.ce('label')
,_save=(str)=>{
  let n=fn.now('jp');
  console.log(n,str)
  l.textContent =n;
}
,save=_.throttle(_save,10*1000)
,on=function(ev){
  o.innerHTML = t.value;
  save(t.value);
}
;[t,l,o].forEach(d=>document.body.appendChild(d))
;
t.oninput =_.debounce(on,70);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?