set Timeout,set Interval
13526 ワード
コード
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=utf-8"
/>
<
title
>
</
title
>
</
head
>
<
body
>
<
h4
>
:
<
span
id
="a"
></
span
></
h4
>
<
script
type
="text/javascript"
>
showTime();
function
showTime(){
var
d
=
new
Date();
var
a
=
document.getElementById(
"
a
"
);
a.innerHTML
=
"
This time
"
+
d.toLocaleString();
setTimeout(
"
showTime()
"
,
1000
);
}
</
script
>
<
h4
>
<
span
id
="content"
></
span
></
h4
>
<
input
type
="button"
value
="start"
onclick
="timer = setInterval('startShow()', 1000)"
/>
<
input
type
="button"
value
="stop"
onclick
="stopShow()"
/>
<
script
type
="text/javascript"
>
var
iValue
=
1
;
//
var timer = null;
var
cont
=
document.getElementById(
"
content
"
);
//
var timer = setInterval("startShow()", 1000);
function
startShow(){
cont.innerHTML
=
cont.innerHTML
+
(iValue
++
)
+
"
-
"
;
//
timer = setTimeout("startShow()", 1000);
}
function
stopShow(){
clearInterval(timer);
}
</
script
>
</
body
>
</
html
>
このエクササイズを通して、setTimeout、set Intervalの使い方をマスターしました.