clearInterval

clearIntervalを利用する事によって、setIntervalで定期的にスケジュールされているタスクを取り消せます。 clearIntervalの引数は、setIntervalが返すハンドルです。

以下のサンプルではclearTimeoutを利用して、setIntervalされる関数を後から取り消しています。 それによって、本来ならば1秒毎に実行される予定である「callbackfunc」が、5秒後に呼び出されるcancelfunc内のclearIntervalによって取り消されています。

widget.js


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

// this will be used by clearTimeout
var to;

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

function cancelfunc() {
  print("clearInterval\n");
  clearInterval(to);
}

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

    to = setInterval(callbackfunc, 1*1000);
    setTimeout(cancelfunc, 5*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);
}


サンプルダウンロード

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


あきみち

「インターネットのカタチ - もろさが織り成す粘り強い世界 -」関連資料

インターネットのカタチ もろさが織り成す粘り強い世界

Linuxネットワークプログラミング

カスタム検索




アルファブロガー・アワード2011受賞ブログ


外部サイト

プレコ王国
ディスカス魂
金魚タイムズ
YouTubeチャネル
Twitter
Facebook
mixi(ほぼ未使用)

フィードメーター - Geekなぺーじ
Copyright (C) Geekなページ.
All rights reserved. 無断転載や無断コピーなど、私的利用の範囲を逸脱した利用はおやめ下さい.