信号機の動作をシミュレートする jQuery プラグインを作成しました。ここでデモとソースコードを見ることができます:
$.fn.plugin = function (b/*time*/,a/*starting with given color*/) {
b.push(b[1]);//adding phases time to plugin array
return this.each(function (f, d) {
var
e = ["#008000", "#FFFF00", "#FF0000", "#FFFF00"],//green,yellow,red,yellow
c = function () {
$(d).html(b[a])// here i want to place time, that left for current phase, it should change dynamicly
.css({
"background-color": e[a]//changing bg as light
});
window.setTimeout(c, 1000 * b[a]);//hold phase as long as we told to plugin
a = ++a % 4//result would be 0..1..2..3 and so on
};
c()
})
};
$(window).load(function(){
$("#c1").plugin([2,2,2],0);
$("#c2").plugin([2,2,2],2);
})
</head>
<body>
Starts with red:<span id="c1" style=""> </span><br>
Starts with green:<span id="c2" style=""> </span><br>
ご覧のとおり、要素を選択し、プラグインを追加し、緑、黄、赤のフェーズの時間を書き、信号機の色を開始する必要があります。
$(d).html(b[a])
コードのこの部分は、現在のフェーズの時間を書いているだけです。この数字は、フェーズの残り時間を示したいと思います。