简体   繁体   English

Javascript将脚本包装成函数

[英]Javascript wrapping script into a function

here i have a simple script which is a countdown timer, here is the code: 这里我有一个简单的脚本,它是一个倒数计时器,这是代码:

<span id="countdown" class="timer"></span>
<script>
    var seconds = 60;
    function secondPassed() {
        var minutes = Math.round((seconds - 30)/60);
        var remainingSeconds = seconds % 60;
        if (remainingSeconds < 10) {
            remainingSeconds = "0" + remainingSeconds;  
        }
        document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
        if (seconds == 0) {
            clearInterval(countdownTimer);
            document.getElementById('countdown').innerHTML = "Buzz Buzz";
        } else {
            seconds--;
        }
    }
    var countdownTimer = setInterval('secondPassed()', 1000);
</script>

Now at the moment when i open this page on chrome, it starts the countdown straight away, what im trying to do is make it start the countdown when i click on an image, how would i do this guys? 现在,当我在chrome上打开此页面时,它立即开始倒计时,我想做的就是当我单击图像时使它开始倒计时,我将如何做呢?

Thanks 谢谢

Start the interval inside an event handler for the image 在图像的事件处理程序中启动间隔

<span id="countdown" class="timer"></span>
<img src="http://www.clker.com/cliparts/5/r/I/j/O/k/another-green-start-button.svg" id="image" />

<script>
    var seconds = 60,
        countdownTimer;

    function secondPassed() {
        var minutes = Math.round((seconds - 30)/60);
        var remainingSeconds = seconds % 60;
        if (remainingSeconds < 10) {
            remainingSeconds = "0" + remainingSeconds;  
        }
        document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
        if (seconds == 0) {
            clearInterval(countdownTimer);
            document.getElementById('countdown').innerHTML = "Buzz Buzz";
        } else {
            seconds--;
        }
    }

    document.getElementById('image').addEventListener('click', function() {
          countdownTimer = setInterval(secondPassed, 1000);
    }, false);

</script>

FIDDLE 小提琴

Use onClick of the image 使用图片的onClick

<span id="countdown" class="timer"></span>
<img onClick="startTimer()" src="https://cdn1.iconfinder.com/data/icons/iconsweets2/40/clock_alarm.png">
<script>
    var seconds = 60;
    function secondPassed() {
        var minutes = Math.round((seconds - 30)/60);
        var remainingSeconds = seconds % 60;
        if (remainingSeconds < 10) {
            remainingSeconds = "0" + remainingSeconds;  
        }
        document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
        if (seconds == 0) {
            clearInterval(countdownTimer);
            document.getElementById('countdown').innerHTML = "Buzz Buzz";
        } else {
            seconds--;
        }
    }
    var countdownTimer;
    function startTimer(){
        countdownTimer = setInterval('secondPassed()', 1000);
    }
</script>

在图像本身上设置一个onclick侦听器:

<img onclick="setInterval('secondPassed()', 1000)" src="..."/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM