为什么我的闹钟脚本停止工作? [英] Why does my alarmclock script stop working?

查看:62
本文介绍了为什么我的闹钟脚本停止工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它只能工作一次。点击第二个按钮,没有任何结果。

It only works once. At second button click, nothing occurs.

如果我在 i_budilnik <更改 budilnik 变量/ code>或 var budilnik ,它甚至不起作用一次!

If I change budilnik variable at i_budilnik or var budilnik, it doesn't work even once!

问题出在哪里?

<div>
<form name="alert">
    <input type="text" name="hour" />
    <input type="text" name="min" />
    <input type="button" value="ok" onclick="budilnik(this.form)">
</form><font color=#660000 size=20 face=Tahoma><span id="hours"></span>
</div>

<script type="text/javascript">


function budilnik(form) {
  budilnik = 1;
  min = form.min.value;
  hour = form.hour.value;
}

obj_hours = document.getElementById("hours");

function wr_hours() {
  time = new Date();

  time_min = time.getMinutes();
  time_hours = time.getHours();
  time_wr = ((time_hours < 10) ? "0" : "") + time_hours;
  time_wr += ":";
  time_wr += ((time_min < 10) ? "0" : "") + time_min;

  time_wr = time_wr;

  obj_hours.innerHTML = time_wr;

  if (i_budilnik == 1) {

      if (min == time_min) {
          if (hour == time_hours) {
              alert('welldone');
              budilnik = 0;
          }
      }
  }
}
wr_hours();
setInterval("wr_hours();", 1000);
</script>


推荐答案

你调用函数wr_hours();只有一次...用onclick budilnik被调用,但那不会再次触及wr_hours。第一次运行代码,因为页面已加载,但在此之后,使用onclick只会再次设置min和hour的值。

You call the function wr_hours(); only once... with the onclick budilnik is called, but that doesn't touch wr_hours again. The first time the code is run, because the page is loaded, but after that, with the onclick only the values of min and hour are set again.

编辑:你不应该把你的表单称为警告,因为这是javascript中的保留字,对于变量min也是如此。另外:变量min和hour在函数 budilnik 中定义,但它们在此范围之外是未知的。我还将变量 budilnik 重命名为全局变量 justonce ,以确保您可以在<$范围之外检查它C $ C> budilnik 。我重写了你的代码:

edit: you shouldn't call your form "alert", since that's a reserved word in javascript, same for the variable min. also: the variables min and hour are defined in the function budilnik, but they're not known outside this scope. I also renamed the variable budilnik to a global variable justonce to make sure you can check it outside the scope of budilnik. I rewrote your code a bit:

<html>
<body>
    <div>
        <form name="frm">
            <input type="text" name="hour" />
            <input type="text" name="mins"/>
            <input type="button" value="ok" onclick="justonce=1;">
        </form>
        <font color=#660000 size=20 face=Tahoma><span id="hours"></span></font>
    </div>
</body>
</html>

<script type="text/javascript">
obj_hours=document.getElementById("hours");
justonce=0;

function wr_hours()
{
    time=new Date();

    time_min=time.getMinutes();
    time_hours=time.getHours();

    time_wr=((time_hours<10)?"0":"")+time_hours;
    time_wr+=":";
    time_wr+=((time_min<10)?"0":"")+time_min;

    obj_hours.innerHTML=time_wr;

    if (justonce==1 && frm.mins.value==time_min && frm.hour.value==time_hours) {
            alert('welldone');
            justonce=0;
    }
}

setInterval("wr_hours();",1000);
</script>

顺便说一句,你的函数wr_hours可能会短得多:

Your function wr_hours could be a lot shorter by the way:

function wr_hours()
{
    time=new Date();

    obj_hours.innerHTML=("%02d",time.getHours())+":"+("%02d",time.getMinutes());

    if (justonce==1
        && frm.mins.value==time.getMinutes()
        && frm.hour.value==time.getHours()) {
        alert('welldone');
            justonce=0;
    }
}

这篇关于为什么我的闹钟脚本停止工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆