Window.setInterval not working from asp.net

  • Thread starter Thread starter barry
  • Start date Start date
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>
 
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
 
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>



-Brock
DevelopMentor
http://staff.develop.com/ballen
 
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.
 
Back
Top