yieldを使って1ステップずつ実行する

説明

1ステップずつ実行するにはyieldを使います。関数内で途中で中断したい箇所でyieldを書きます。中断した後、実行を再開するにはnext()メソッドを呼び出します。
yieldはFirefox 2以降で利用できますが、利用する際にはscriptタグのtype属性でバージョン1.7を明示しておく必要があります。

サンプルプログラム

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Sample</title>
<link rel="stylesheet" href="css/main.css" type="text/css" media="all">
<script type="text/javascript;version=1.7" src="js/sample.js"></script>
</head>
<body>
<h1>ステップ実行</h1>
<form method="get" action="./regist.cgi">
<input type="button" value="実行" id="execButton">
</form>
<div id="result"></div>
</body>
</html>

【スクリプト】
window.onload = function(){
var genObj = add();
document.getElementById("execButton").onclick = function(){ genObj.next(); };
}
function add(){
for (var i=0; i<99; i++){
document.getElementById("result").innerHTML = i;
yield;
}
}
サンプルを実行
[戻る]