Skip to content

Instantly share code, notes, and snippets.

@josejuan
Created July 19, 2012 09:25
Show Gist options
  • Save josejuan/3142648 to your computer and use it in GitHub Desktop.
Save josejuan/3142648 to your computer and use it in GitHub Desktop.
bigJob
// Calculamos Pi enviando datos cada X tiempo
function Pi(MaxMilliSegs) {
var T, H, t, r = 0;
var rst = function () {
T = H = 0;
t = MaxMilliSegs + ~~new Date();
};
rst();
while(1) {
// para no andar mirando el tiempo
if(++r > 10000) {
// notificamos?
if(~~new Date() > t) {
postMessage({T: T, H: H});
rst();
}
r = 0;
}
// calculamos hit
++T;
var x = Math.random();
var y = Math.random();
if(x * x + y * y < 1.0) H++;
}
}
// nos ponemos en marcha y no paramos
onmessage = function(args) {
Pi(args.data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment