javascript 倒數計時 按鈕

我係google 搵過..但始終搵唔到岩用

有無大哥可以良一下

我想係..要手動按左按鈕--->按鈕會倒數-->之後就彈出一個網頁 或 會在我設定有下角框架出現我指定網頁 ,thanks

<script type='text/javascript'>
var time=10000;//設定倒數10秒
function DisableEnable(objid){
        if(time<=0){
                document.getElementById(objid).value='You can click again';
                document.getElementById(objid).disabled=false;
        }else{
                document.getElementById(objid).disabled = true;
                document.getElementById(objid).value = (time/1000) + " sec...";
                setTimeout("DisableEnable('" + objid + "')",1000);
        }
        time-=1000;
}
</script>

<body>
<input type=button value="Click me" id=button1 onclick=DisableEnable(this.id)>
</body>
[/code]

回復 1# uhang

should have used time event/handler
http://www.w3schools.com/js/js_timing.asp

and note that javascript timer event is not that accurate
http://www.sitepoint.com/creating-accurate-timers-in-javascript/

TOP

Try this below

<input id="countdown-button" type="button" value="Start" onclick="startCountDown()" />
<script>

        // Number of counts
        var count = 10;
        // How many second per count
        var duration = 1000; // 1 sec
       
        // Fucnction start the countdown
        var startCountDown = function()
        {
                var e = document.getElementById('countdown-button'); // Button reference
                e.value = count;
                e.disabled = true;
               
                // Loop by setTimeout until the requirement satisfy
                var loop = function(){
                        e.value = --count; // Take one count at a time
                        if ( count > 0 ) // Not meet requirement, Continue the loop
                                setTimeout(loop, duration);
                        else // Done
                        {
                                // *** Do whatever you want to do below when done. ***
                                e.value = 'Done';
                                //window.open('http://www.google.com');
                        }
                };
                setTimeout(loop, duration);
        }
       
        // Enable the button initially
        document.getElementById('countdown-button').disabled = false;
       
       
</script>

TOP

成功了 ! 我想問有無可能在倒數時加個中文字係
餘下 X 秒  謝謝

TOP

回復 4# uhang

yes. have a <div> with an ID, then in your loop function (the function that is invoked by the timer), do a getElementByID and modifer its inner HTML

TOP

var loop = function(){
                        e.value = '餘下 '+(--count)+' 秒'; // Take one count at a time
                        if ( count > 0 ) // Not meet requirement, Continue the loop
                                setTimeout(loop, duration);
                        else // Done
                        {
                                // *** Do whatever you want to do below when done. ***
                                e.value = 'Done';
                                //window.open('http://www.google.com');
                        }
                };

TOP

var loop = function(){
                        e.value = '餘下 '+(--count)+' 秒'; // Take one count  ...
lancon13 發表於 2012-11-29 16:54



   試過加了@@好像不行,

TOP

回復 7# uhang

What do you mean not working? Is this not what you want to do?
http://jsfiddle.net/NkUpP/

TOP

多謝分享, 我也要用到這個 javascript 在我的 website 內

TOP

分謝 成功了

TOP