Window.setInterval not working from asp.net

B

barry

If The script below is exeuted from the onload="startit()" in the body tag everything works fine but when I remove the onload and try to execute by using the attributes.add from within the Page_Load the window.setInterval will not execute - there is no message but the timer never takes off. I put an alert in the startit() and it did appear so the attributes.add is working ok.

Would appreciate any help

thanks



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Me.Button3.Attributes.Add("onClick", "startit()")
End Sub


<SCRIPT>
function startit() {
window.setInterval("updateTime()",1000);

}
var seconds=0;

function updateTime() {

// seconds++;
var timeNow = new Date();
var hours = timeNow.getHours();
var minutes = timeNow.getMinutes();
var seconds = timeNow.getSeconds();
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue = ((timeValue <10)? "0":"") + timeValue
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " PM" : " AM"
document.getElementById("time").innerHTML = timeValue;
}
</SCRIPT>
</HEAD>
<body MS_POSITIONING="GridLayout" onload="startit()">
<h1>Sample Document</h1>
<h3>You have been here <b id="time">0</b> seconds.
</h3>
 
B

Brock Allen

I don't know what the <body> issue is, but in Page_Load try:

Page.RegisterStartupScript("mystartscript", "startit();")
 
B

barry

I tried your suggestion and put alerts into both functions and while the
first function which has the window.setInterval showed the alert the second
function being executed from the setInterval was never executed. No errors
appear.

I also put the code into my code behind but received the same results.

thanks
 
B

Brock Allen

Yeah, sorry. I gave you bad javascript for RegisterStartupScript:

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterStartupScript("MyInit", "<script>DoInit();</script>");
}
</script>
<script>
<!--
function DoInit()
{
window.setInterval('foo()', 500);
}
function foo()
{
document.getElementById('myLabel').innerHTML += 'x';
}
-->
</script>
 
B

barry

That was the answer. One thing to add. I put the RegisterStartupScript in
the button click event so it would not start automatically but wait until I
hit the button.

Thanks a lot. This really helps with understanding asp.net and javascript
relationships and using client side programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top