2016-12-30 63 views
-1

我可以用JavaScript製作一些能夠加快速度的東西嗎?JavaScript讓時間過得更快

像JS傳十分鐘的時候,1本現實世界的第二次

+0

A排序所有的延遲你的js呢? –

+1

你需要什麼? – akuhn

+5

javascript是由Brenden Eich編寫的,而不是HG Wells –

回答

3

可以覆蓋的setTimeout和setInterval和日期:

function TimeMachine(timescale,code){ 
timescale=timescale || 0.1;//ten times faster 

var context=Object.create(window); 
//setTimeout 
context.setTimeout=var setTimeout=function(callb,time){ 
    window.setTimeout(callb,time*timescale); 
}; 

//setInterval 
context.setInterval=var setInterval=function(callb,time){ 
    window.setInterval(callb,time*timescale); 
}; 

//Date TODO:resolve timestrings 
var now=new window.Date().getTime(); 
window.setInterval(function(){now+=1/timescale;},1); 
context.Date=var Date=function(){ 
this.getTime=function(){ 
    return now; 
}; 
} 

//create Scope with new window 
(function(window){ 
eval("("+code+")()"); 
})(context); 
}; 

使用這樣的:

TimeMachine(0.1,function(){ 
//execute in time machines context 
setInterval(function(){alert("10 seconds passed");},10000);//10secs -> 1sec 

//even this should work: 
window.setTimeout(function(){alert("test")},1000); 
}); 

香港專業教育學院建立了一個時間機器:0

參考文獻: http://www.instructables.com/id/How-to-Build-a-Time-Machine-Vortex-Distortion-Spa/

0

在這裏,我創建了一個函數,當有兩個參數。第一個參數是現實世界時間,第二個參數是它對應的虛擬時間。

$v_time(1000,100); // Maps 1 second of real world as 100ms in virtual world 

使用兩個參數調用函數來映射它們並創建虛擬時間世界。

$v_time(60000) // returns virtual world time corresponding to real world 60 sec; 

現在,只要你需要虛擬時間調用該函數與你想要的時間。

$v_time=function (rel_time,vir_time) { 
 
    if(vir_time) $v_time.offset 
 
        = (Math.min(rel_time,vir_time) 
 
         /Math.max(rel_time,vir_time)); 
 
    return rel_time*$v_time.offset; 
 
} 
 

 
$v_time(1000,100); // Maps 1sec of real world as 100ms of virtual world 
 

 
setTimeout(function(){ 
 
      console.log('hello'); 
 
      },$v_time(10000)); // 10sec of real world will return 1s of virtual world.

做10分鐘真實世界的虛擬世界1秒

$v_time(10*60*1000,1000); 

現在叫$v_time(your_time);