LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Sunday, January 14, 2018

setTimeoutに引数を渡したい


setTimeout で指定した秒数後に実行する関数に
引数を渡したい時に試したコードです

渡す方法
  • setTimeout(function(){ alert(message); }, 2000);
  • setTimeout("alert('" + message + "')", 2000);

上記のどちらでも渡せますが
コード.gsでは見やすいこちらの方で書きました

var message = "Hello";
setTimeout(function(){ alert(message); }, 2000);




このボタンのように
クリックした2秒後にアラートが表示されます



コード.gs
function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}
意訳
この機能がやること
指定したHTMLファイルを表示する




index.html
<!DOCTYPE html>
<html>
<body>
  <button id="bt">ボタン</button>
  <script>
    document.getElementById("bt").onclick = bt_clicked;

    function bt_clicked() {
      var message = "Hello";
      setTimeout(function () {
        alert(message);
      }, 2000);
    }
  </script>
</body>
</html>
意訳
これはHTML5文書です


ボタンを置く

idがbtの要素がクリックされたらbt_clickedを実行する

この機能がやること
メッセージを作って
設定した時間後に
アラートに出す
設定する時間は2000ミリ秒後






参考

Window setTimeout() Method
https://www.w3schools.com/jsref/met_win_settimeout.asp

Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...