Login   Register  
PHP Classes
elePHPant
Icontem

File: class/class.noTimeOut.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Johan Barbier  >  No Timeout  >  class/class.noTimeOut.js  >  Download  
File: class/class.noTimeOut.js
Role: Auxiliary data
Content type: text/plain
Description: noTimeOut js class
Class: No Timeout
Split the execution of long tasks in small steps
Author: By
Last change: Fixed js TIMEOUT bug
Date: 7 years ago
Size: 16,128 bytes
 

Contents

Class file image Download
/**
@author : Johan Barbier <johan.barbier@gmail.com>
@Version : 2006/10/20
*/
function noTimeOut () {

	var aStack = new Array;
	var aStacks = new Array;

	function getObject () {
		if (window.XMLHttpRequest) {
			var oXmlhttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			var oXmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return oXmlhttp;
	}

	function addToData (sStack) {
		var sData = '';
		var iArgs = aStack[sStack]['ARGS'].length;
		if ( iArgs > 0) {
			for (var iCpt = 0; iCpt < iArgs; iCpt ++) {
				sData += '&arg_'+iCpt+'='+aStack[sStack]['ARGS'][iCpt];
			}
		}
		return sData;
	}

	function getDefault (sStack, iStart) {
		var j = iStart + aStack[sStack]['STEP'];
		aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
		aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
			if (aStack[sStack]['OXMLHTTP'].readyState==1) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==2) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
				}
				/**
				* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
				*
				*/
				if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
					var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
					parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
				} else {
					parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
				}
				/*
				parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
				*/
				if (j < aStack[sStack]['LIMIT']) {
						aStacks.unshift (sStack);
						aStack[sStack]['START'] = j;
						checkStack ();
				}
			}
		}
		aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = 'sType=DEFAULT&iStart='+iStart+'&iStep='+STEP;
		var iArgs = aStack[sStack]['ARGS'].length;
		data += addToData (sStack);
		aStack[sStack]['OXMLHTTP'].send (data);
	}

	function getDB (sStack, iStart) {
		var j = iStart + aStack[sStack]['STEP'];
		aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
		aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
			if (aStack[sStack]['OXMLHTTP'].readyState==1) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];

				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==2) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];

				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];

				}
				/**
				* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
				*
				*/
				if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
					var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
					parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
				} else {
					parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
				}
				//parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
				if (j < aStack[sStack]['LIMIT']) {
						aStacks.unshift (sStack);
						aStack[sStack]['START'] = j;
						checkStack ();
				}
			}
		}
		aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = 'sType=DB&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sQuery='+aStack[sStack]['QUERY'];
		data += addToData (sStack);
		aStack[sStack]['OXMLHTTP'].send (data);
	}

	function getFile (sStack, iStart) {
		var j = iStart + aStack[sStack]['STEP'];
		aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
		aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
			if (aStack[sStack]['OXMLHTTP'].readyState==1) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==2) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
				}
				/**
				* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
				*
				*/
				if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
					var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
					parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
				} else {
					parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
				}
				/*
				parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
				*/
				if (j < aStack[sStack]['LIMIT']) {
						aStacks.unshift (sStack);
						aStack[sStack]['START'] = j;
						checkStack ();
				}
			}
		}
		aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = 'sType=FILE_LINE&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
		data += addToData (sStack);
		aStack[sStack]['OXMLHTTP'].send (data);
	}

	function getFileLine (sStack, iStart) {
		var j = iStart;
		aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
		aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
			if (aStack[sStack]['OXMLHTTP'].readyState==1) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==2) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
				}
				/**
				* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
				*
				*/
				if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
					var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
					parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
				} else {
					parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
				}
				/*
				parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
				*/
				j += aStack[sStack]['OXMLHTTP'].responseText.length;
				if (aStack[sStack]['SUBSTR'] != '') {
					j -= aStack[sStack]['SUBSTR'];
				}
				if (j < aStack[sStack]['LIMIT']) {
						aStacks.unshift (sStack);
						aStack[sStack]['START'] = j;
						checkStack ();
				}
			}
		}
		aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = 'sType=FILE_LINE&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
		data += addToData (sStack);
		aStack[sStack]['OXMLHTTP'].send (data);
	}

	function getFilePat (sStack, iStart) {
		var j = iStart;
		aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
		aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
			if (aStack[sStack]['OXMLHTTP'].readyState==1) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==2) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
				}
				/**
				* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
				*
				*/
				if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
					var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
					parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
				} else {
					parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
				}
				/*
				parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
				*/
				j += aStack[sStack]['OXMLHTTP'].responseText.length;
				if (aStack[sStack]['SUBSTR'] != '') {
					j -= aStack[sStack]['SUBSTR'];
				}
				if (j < aStack[sStack]['LIMIT']) {
						aStacks.unshift (sStack);
						aStack[sStack]['START'] = j;
						checkStack ();
				}
			}
		}
		aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = 'sType=FILE_PATTERN&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
		data += addToData (sStack);
		aStack[sStack]['OXMLHTTP'].send (data);
	}

	function oneShot (sStack) {
		aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
		aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
			if (aStack[sStack]['OXMLHTTP'].readyState==1) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];

				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==2) {
				if (aStack[sStack]['MSG'] != '') {
					parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];

				}
			}
			if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
				/**
				* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
				*
				*/
				/*
				if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
					var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
					parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
				} else {
					parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
				}
				*/
				parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML = aStack[sStack]['OXMLHTTP'].responseText;
				checkStack ();
			}
		}
		aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		data = addToData (sStack);
		aStack[sStack]['OXMLHTTP'].send (data);
		checkStack ();
	}

	function getData (sStack, sType) {
		var bGo = false;
		switch (sType) {
			case 'DEFAULT':
				getDefault (sStack, aStack[sStack]['START']);
				break;
			case 'FILE_OCTET':
				getFile (sStack, aStack[sStack]['START']);
				break;
			case 'FILE_LINE':
				getFileLine (sStack, aStack[sStack]['START']);
				break;
			case 'FILE_PATTERN':
				getFilePat (sStack, aStack[sStack]['START']);
				break;
			case 'DB':
				getDB (sStack, aStack[sStack]['START']);
				break;
			case 'ONE_SHOT':
				oneShot (sStack);
				break;
		}
	}

	function checkStack () {
		var iLen = aStacks.length;
		if (iLen > 0) {
			var sStack = aStacks[iLen - 1];
			__checkStack = function () {
					var sType = aStack[aStacks[iLen - 1]]['TYPE'];
					aStacks.pop ();
					getData (sStack, sType);
				}
			if (aStack[sStack]['TIMEOUT'] > 0) {
				setTimeout ('__checkStack()', aStack[sStack]['TIMEOUT'] );
			} else {
				__checkStack (sStack, iLen);
			}
		}
	}

	this.declareStack = function (sStack) {
		aStack[sStack] = new Array;
		aStack[sStack]['START'] = aStack[sStack]['LIMIT'] = aStack[sStack]['STEP'] = aStack[sStack]['FILE'] = aStack[sStack]['QUERY'] = aStack[sStack]['TARGET'] = aStack[sStack]['SCRIPT'] = aStack[sStack]['MSG'] = aStack[sStack]['SUBSTR'] = '';
		aStack[sStack]['METHOD'] = 'POST';
		aStack[sStack]['TIMEOUT'] = 0;
		aStack[sStack]['ARGS']  = new Array;
		aStack[sStack]['MSG_WAITING'] = 'Loading';
		aStack[sStack]['MSG_LOADED'] = 'Loaded';
		aStack[sStack]['MSG_READY'] = 'OK';
	}

	this.startWork = function (sStack) {
		aStack[sStack]['OXMLHTTP'] = getObject ();
		var iLen = aStacks.length;
		aStacks[iLen] = sStack;
		checkStack ();
	}

	this.initialize = function (sStack, sType, mValue) {
		if (!aStack[sStack]) {
			return false;
		}
		switch (sType) {
			case 'START' :
				aStack[sStack]['START'] = mValue;
				return true;
				break;
			case 'LIMIT' :
				aStack[sStack]['LIMIT'] = mValue;
				return true;
				break;
			case 'STEP' :
				aStack[sStack]['STEP'] = mValue;
				return true;
				break;
			case 'SUBSTR' :
				aStack[sStack]['SUBSTR'] = mValue;
				return true;
				break;
			case 'FILE' :
				aStack[sStack]['FILE'] = mValue;
				return true;
				break;
			case 'QUERY' :
				aStack[sStack]['QUERY'] = mValue;
				return true;
				break;
			case 'TARGET' :
				aStack[sStack]['TARGET'] = mValue;
				return true;
				break;
			case 'SCRIPT' :
				aStack[sStack]['SCRIPT'] = mValue;
				return true;
				break;
			case 'MSG' :
				aStack[sStack]['MSG'] = mValue;
				return true;
				break;
			case 'MSG_WAITING' :
				aStack[sStack]['MSG_WAITING'] = mValue;
				return true;
				break;
			case 'MSG_LOADED' :
				aStack[sStack]['MSG_LOADED'] = mValue;
				return true;
				break;
			case 'MSG_READY' :
				aStack[sStack]['MSG_READY'] = mValue;
				return true;
				break;
			case 'METHOD' :
				if (mValue != 'POST' || mValue != 'GET') {
					return false;
				}
				aStack[sStack]['METHOD'] = mValue;
				return true;
				break;
			case 'TYPE':
				aStack[sStack]['TYPE'] = mValue;
				break;
			case 'ARGS':
				aStack[sStack]['ARGS'] = mValue;
				break;
			case 'TIMEOUT':
				mValue = parseInt (mValue);
				if (false == isNaN (mValue)) {
					aStack[sStack]['TIMEOUT'] = mValue;
				} else {
					return false;
				}
				break;
			default:
				return false;
				break;
		}
	}
}