Commit 03d57c2a authored by François Billioud's avatar François Billioud

add timing methods in tools.js

parent 5367e7fe
......@@ -33,12 +33,94 @@ UngObject.prototype.inherits = function(superClass) {
*/
currentTime = function() {return (new Date()).toUTCString();}
// save
saveXHR = function(address) {
//create request
var xhr=null;
try
{
xhr = new XMLHttpRequest();
} catch(e)
{
try {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e2)
{
try {xhr = new ActiveXObject("Microsoft.XMLHTTP");}
catch (e) {alert("Please install a more recent browser")}
}
}
//xhr.open("PUT", keyToUrl(key, wallet), true, wallet.userAddress, wallet.davToken);
//HACK:
xhr.open("PUT", address, true);
xhr.setRequestHeader("Authorization", "Basic "+"nom:test");
//END HACK.
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status != 200 && xhr.status != 201 && xhr.status != 204) {
alert("error: got status "+xhr.status+" when doing basic auth PUT on url "+Base64.encode("nom:test")+" " + xhr.statusText);
}
}
}
xhr.withCredentials = "true";
xhr.send(JSON.stringify(getCurrentDocument()));
}
// load
loadXHR = function(address) {
//create request
var xhr=null;
try
{
xhr = new XMLHttpRequest();
} catch(e)
{
try {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e2)
{
try {xhr = new ActiveXObject("Microsoft.XMLHTTP");}
catch (e) {}
}
}
xhr.open("GET", address, true);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
var cDoc = getCurrentDocument();
if(xhr.status == 200) {
cDoc.load(JSON.parse(xhr.responseText));
} else {
alert("error: got status "+xhr.status+" when doing basic auth GET on url "+"nom:test"+" " + xhr.statusText);
}
cDoc.setAsCurrentDocument();
}
}
xhr.send();
}
/*
* wait an event before execute an action
*/
waitBeforeExecution = function(event, func) {
var waitBefore = function() {
if(event) {func.call();} else {setTimeout(waitBefore,1000)}
tryUntilSucceed = function(func) {
var nb = 2;//avoid to test too much times
var retry = function() {
try {return func.call();}
catch(e) {if(nb<100) {setTimeout(retry,nb*100); alert(e);}}
nb*=nb;
}
setTimeout(waitBefore,1000);
retry();
}
requireBeforeSucceed = function(required, func) {
var test = function() {
try {return required.call();}
catch(e) {return null;}
}
if(test()) {tryUntilSucceed(func);} else {setTimeout(test,100);};
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment