// vi:set ft=javascript ff=dos ts=4 sts=4 sw=4 et:

// This sample create two timers
// Click the window to start the timer, and see the result after the console is showed.
// Click the window again to kill all timers.

// ==PREPROCESSOR==
// @name "Timer"
// @author "T.P Wang"
// ==/PREPROCESSOR==

var g_timer_started = false;
var g_count = 0;
var g_timer1_ID, g_timer2_ID;

function print_to_console(msg) {
    fb.trace("Timer test:", msg);
}

function on_mouse_lbtn_up() {
    if (!g_timer_started) {
        // Timer are created here
        // 2s - one shot, happens after 2000 ms, only once
        g_timer1_ID = window.SetTimeout(function() { 
            // Print and show console
            fb.ShowConsole();
            print_to_console("g_timer1: Show console now.");
        }, 2000); 
        
        // 500ms - periodic, happens every 500 ms
        g_timer2_ID = window.SetInterval(function() {
            g_count++;
            print_to_console("g_timer2: " + g_count + " time(s).");
        }, 500);
        
        g_timer_started = true;
    } else {
        // Kill all timers
        window.ClearTimeout(g_timer2_ID);
        window.ClearInterval(g_timer1_ID);
        g_timer_started = false;
        g_count = 0;
        print_to_console("Timers killed.");
    }
}
