﻿// 當 window unload ，停止 service 
var ajaxRequestExecutors = [];
$(function () {
	// 測試用
	//$('body').prepend("<input type='button' value='abort' onclick='window_unload()' />");
	Sys.Net.WebRequestManager.add_invokingRequest(OnInvokingRequest);
	$(window).bind("beforeunload", unloadAction);

	function OnInvokingRequest(sender, args) {
		try {
			ajaxRequestExecutors.push(args.get_webRequest().get_executor());
		}
		catch (e) { }
	}

	// 測試用
	function unloadAction() {
		try {
			// wsTimer is global variable
			if (typeof (wsTimer) != "undefined")
				window.clearTimeout(wsTimer);
			_wsQueue = [];
			for (var i = 0; i < ajaxRequestExecutors.length; i++) {
				var e = ajaxRequestExecutors[i];
				if (e.get_started() && !e.get_aborted() && !e.get_responseAvailable()) {
					e.abort();
				}
			}
		}
		catch (e) { }
	}
});


