2012-02-07 64 views
1

我有这个JavaScript的功能,倒计时直到你可以点击下载按钮,但是我想它一旦点击它,要么再次禁用或开始倒计时,最好是禁用。重新启动计数器点击或禁用它 - Javascript

这是JavaScript。

<script type="text/javascript"> 
    x = 10; 
    function countdown() { 
    if (x > 1) { 
     x--; 
     document.getElementById("button").innerHTML = x; 
     r = setTimeout("countdown()",1000); 
    } 
    else { 
     clearTimeout(r); 
     document.getElementById("button").innerHTML = "Click to download"; 
     document.getElementById("button").disabled = ""; 
    } 
    } 
    r = setTimeout("countdown()",1000); 

</script> 

这里是按钮,

<label><font size="5">Your download will start in: </label><a href="download.php?shortURL=<?php echo $fullfile; ?>"><button class="btn orange" id="button" disabled="disabled" onclick="window.location.reload()" >10</button></font></a> 

谢谢!

+0

? – 2012-02-07 18:37:15

+1

请注意,您的HTML格式不正确 - ''标签必须在关闭'

+0

没问题,我只是想当你点击按钮时,让柜台再次启动 – HarryBeasant 2012-02-07 18:40:21

回答

1

我相信这是你需要的代码:什么是给你的问题

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title></title> 
    <script type="text/javascript"> 
     var x = 10; 
     var r = 0; 
     function countdown() { 
      if (x > 1) { 
       x--; 
       document.getElementById("button").innerHTML = x; 
       r = setTimeout("countdown()",1000); 
      } 
      else { 
       clearTimeout(r); 
       document.getElementById("button").innerHTML = "Click to download"; 
       document.getElementById("button").disabled = ""; 
       document.getElementById("button").onclick = function() { 
        document.getElementById("button").disabled = "disabled"; 
       }; 

      } 
     } 
     setTimeout("countdown()",1000); 
    </script> 
</head> 
<body> 
<label> 
    <font size="5">Your download will start in:</font> 
</label> 
<a href="download.php?shortURL=<?php echo $fullfile; ?>" target="_blank"> 
    <button class="btn orange" id="button" disabled="disabled">10</button> 
</a> 
</body> 
</html> 
+0

是的,这工作得很好,还有一件事,我怎么能说,在按钮“下载将开始于:”10“,但只有数字改变? – HarryBeasant 2012-02-07 19:17:04

+0

我编辑了代码对不起: ) – tftd 2012-02-07 20:00:49

3

要重新开始倒计时,你可以创建一个函数来从onclick事件的按钮,清除任何现有的定时器和重置计数器变量,然后调用这个函数:

function resetCountdown() { 
    clearTimeout(r); 
    x = 10; 
    countdown(); 
} 

要禁用按钮,您可以将其设置为禁用:

document.getElementById("button").disabled = "disabled"; 
1

首先为什么UR使用window.location.reload()..它是什么关系到你的脚本?如果不是将其删除,并尝试下面的jQuery代码

 $(function(){ 
    $('#button').click(function(){ 
     $(this).attr("disabled",true); 
    }); 

,并确保包括jQuery的副本,以使其工作或包含在你的脑袋标签下面的链接这个脚本

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
+0

为什么你发布了一个链接到1.3.2版本的jQuery? :)) – Cristy 2012-02-07 18:44:57

+0

嘿cristy ..它不是一个硬核脚本,甚至可以使用版本1,我有这个链接在一个我的文件,所以只是从那里复制...你自由1.7.1 :-) – 2012-02-07 18:47:46

0

前没有测试,但尝试这一点。一旦显示“点击下载”后点击该按钮,该按钮将被禁用。

<script type="text/javascript"> 
x = 10; 
function countdown() { 
    if (x > 1) { 
     x--; 
     document.getElementById("button").innerHTML = x; 
     r = setTimeout("countdown", 1000); 
    } else { 
     clearTimeout(r); 
     document.getElementById("button").innerHTML = "Click to download"; 
     document.getElementById("button").disabled = ""; 
     document.getElementById("button").onclick = function() { 
      document.getElementById("button").disabled = "disabled"; 
     }; 
    } 
} 
r = setTimeout("countdown",1000); 
</script> 
0

这应该做的伎俩:

var x = 10; 
var r = setInterval(
    function() { 
    if (x > 1) { 
     x--; 
     document.getElementById("button").innerHTML = x; 
    } 
    else { 
     clearInterval(r); 
     document.getElementById("button").innerHTML = "Click to download"; 
     document.getElementById("button").disabled = ""; 
    } 
}, 1000);