By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Window.setInterval not working from asp.net

P: n/a
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>
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


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

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

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


Nov 19 '05 #2

P: n/a
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
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:34**********************@msnews.microsoft.com ...
I don't know what the <body> issue is, but in Page_Load try:

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

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


Nov 19 '05 #3

P: n/a
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
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
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:34**********************@msnews.microsoft.com ...
I don't know what the <body> issue is, but in Page_Load try:

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

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


Nov 19 '05 #4

P: n/a
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.

"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:34**********************@msnews.microsoft.com ...
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
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
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:34**********************@msnews.microsoft.com ...
I don't know what the <body> issue is, but in Page_Load try:

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

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


Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.