Connecting Tech Pros Worldwide Forums | Help | Site Map

Window.setInterval not working from asp.net

barry
Guest
 
Posts: n/a
#1: Nov 19 '05
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>

Brock Allen
Guest
 
Posts: n/a
#2: Nov 19 '05

re: Window.setInterval not working from asp.net


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


[color=blue]
> 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>[/color]



barry
Guest
 
Posts: n/a
#3: Nov 19 '05

re: Window.setInterval not working from asp.net


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" <ballen@NOSPAMdevelop.com> wrote in message
news:344668632480771575352000@msnews.microsoft.com ...[color=blue]
> 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
>
>
>[color=green]
> > 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>[/color]
>
>
>[/color]


Brock Allen
Guest
 
Posts: n/a
#4: Nov 19 '05

re: Window.setInterval not working from asp.net


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


[color=blue]
> 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" <ballen@NOSPAMdevelop.com> wrote in message
> news:344668632480771575352000@msnews.microsoft.com ...[color=green]
>> 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[color=darkred]
>>> 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>[/color][/color][/color]



barry
Guest
 
Posts: n/a
#5: Nov 19 '05

re: Window.setInterval not working from asp.net


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" <ballen@NOSPAMdevelop.com> wrote in message
news:347241632481247933942720@msnews.microsoft.com ...[color=blue]
> 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
>
>
>[color=green]
> > 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" <ballen@NOSPAMdevelop.com> wrote in message
> > news:344668632480771575352000@msnews.microsoft.com ...[color=darkred]
> >> 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>[/color][/color]
>
>
>[/color]


Closed Thread