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

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

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

function print_to_console(msg) {
	console.log('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.');
	}
}
