2017-02-02 44 views
0

我正在使用HTML畫布來通過Euler Forward方法建立微分方程。我使用的JavaScript的setInterval調用函數作爲我的代碼如下從javascript setInterval函數接收值?

<script> 
function step(theta,v) { 
    //calculate values of theta and v 
    //output result 
    return theta,v; 
} 

setInterval(step,0.001); 
</script> 

但我希望函數輸出V和θ,所以我可以給他們回功能的第二次迭代,然後再次第三次迭代等等。那麼如何定期調用函數並接收函數的輸出呢?

回答

1

你正在尋找的東西像下面這樣:

let currentParams ={v: 1, theta: 5}; 

function step(v, theta) { 
    return {v: v + theta, theta: v-theta }; // just an example of operation that should be done 
} 

setInterval(() => { 
    currentParams= step(currentParams.v , currentParams.theta); 
}, 1); 
  • 使用文本對象的數據結構來收集既vΘ(THETA)。

    ↪{V:<initialValue>「Θ」:<initialValue>}

  • 在的setInterval

    ,的step輸入將是文字對象,輸出應該是相同的結構將被分配再次。

+0

使用'clearInterval'來停止'setInterval'。 –