views:

54

answers:

1

it's work only once. at second button click, occured nothing.

if I change budilnik variable at i_budilnik or var budilnik, occured nothing at first and other clicks!

Where is the problem?

<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>
+2  A: 

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.

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>

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;
    }
}
Marga Keuvelaar
this script must call function on click. but it is not work too. why??function budilnik(form){ budilnik=1; min=form.min.value; hour=form.hour.value; wr_hours(); setInterval("wr_hours();",1000); }
butteff
peraphs don't forget to change the setinterval to **setInterval("wr_hours",1000);** without parentheses!
aSeptik
edit source does not work on my computer. May be i must install something??? I think, that code is exelent, may be my computer is incorrect?
butteff
I simplified the code some more, and tested it in both safari and firefox. You really shouldn't need to install anything... just copy this into an empty html-file and call from within your browser! please let me know if it works!
Marga Keuvelaar
I tested it in firefox3.5 and opera 9.0win7I copy your source in clear html file. but it does not want to work!And I can't understand why
butteff
I uploaded the file, so we can see if your editor or your browser is creating the problem. Please go to http://www.obviousmatter.com/testso/hours2.html to see if it works.
Marga Keuvelaar
It's work!!!may be it did not work, because <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />automatically add in my editor?Thanks alot!
butteff
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">this string in my editor doesn't give work to this script
butteff
I shortened your function wr_hours for a bit...
Marga Keuvelaar