setTimeout

setTimeoutを利用する事によって、一定時間後に特定の関数を呼び出せます。 setTimeoutの第一引数が呼び出される関数で、2番目の引数が時間(msec単位)です。 1秒以下の値は1秒に切り上げられます。

以下のサンプルでは、3秒後に"hoge"という文字列をprintしています。

なお、setTimeoutはsetIntervalを含めて同時に3つまでしか設定できません。 4つ目を行っても、セットした関数は呼び出されないので、必要に応じてclearTimeoutを行いましょう。

layout.xml


<?xml version="1.0" encoding="UTF-8"?>
<Widget>
  <Component name="widget">
    <Bitmap name="initial-bg"/>
    <Component name="normal">
      <Bitmap name="normalBase"/>
    </Component>
    <Component name="focus" visible="0">
      <Bitmap name="focusBase"/>
    </Component>
    <Component name="active" visible="0">
    </Component>
  </Component>
</Widget>

widget.js


var nodeNormal     = getNode("normal");
var nodeNormalBase = getChildNode(nodeNormal, "normalBase");
var nodeFocus      = getNode("focus");
var nodeFocusBase  = getChildNode(nodeFocus, "focusBase");

function callbackfunc() {
  print("hoge\n");
}

function onLoad() {
    loadImage(nodeNormalBase, "./parts/normal.png");
    loadImage(nodeFocusBase,  "./parts/focus.png");

    setTimeout(callbackfunc, 3*1000);
}

function onUpKey() {
}

function onDownKey() {
}

function onRightKey() {
}

function onLeftKey() {
}

function onConfirmKey(type) {
}

function onFocus() {
  setVisible(nodeNormal, 0);
  setVisible(nodeFocus, 1);
}

function onUnfocus() {
  setVisible(nodeNormal, 1);
  setVisible(nodeFocus, 0);
}

function onActivate() {
  setVisible(nodeNormal, 0);
  setVisible(nodeFocus, 0);
}


サンプルダウンロード

ウィジェットバンドル : setTimeout.zip

IPv6基礎検定

YouTubeチャンネルやってます!