Reputation: 71
I am trying to show a clock on a button click but and calling set timeout event to continuously show the current time. but it shoes only a single time whenever i click the button. and if if i click after some time then it also work but do not show continuously changing time.
script
function showTime() {
var dat = new Date();
var h = dat.getHours();
var m = dat.getMinutes();
var s = dat.getSeconds();
var tim = h + " :" + m + ":" + s;
document.getElementById('MainContent_Label1').innerHTML = tim;
document.getElementById('MainContent_TextBox1').value = tim;
var t = setTimeout(showTime(), 1000);
}
function Button2_onclick() {
showTime();
}
asp page
asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button2" type="button" value="button" onclick="return Button2_onclick()" /><asp:Button ID="Button1" runat="server" Text="Button"
onclientclick="showTime()" /><br />
<br />
</asp:Content>
Upvotes: 0
Views: 898
Reputation: 2477
try this:
var t = setTimeout("showTime()", 1000);
or you can use setInterval() function, for repetitive task.
Like this:
<input id="Button2" type="button" value="button" onclick="return Button2_onclick()" /><asp:Button ID="Button1" runat="server" Text="Button"
onclientclick="setInterval('showTime()', 1000);" />
Or use it in function as you like. Then you'll not need to set the timeout after every run.
Upvotes: 1
Reputation: 32286
You should use var t = setTimeout(showTime, 1000);
instead.
Upvotes: 5