Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 10th, 2004, 01:20 AM
Authorized User
 
Join Date: Apr 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to pigtail Send a message via Yahoo to pigtail
Default Can I use SetInterval to refresh recordset?

I have a JavaScript that uses setInterval to call a function every 5 seconds. This function reads a record from the database, use DOM to replace the data in my table. I added a column that shows a running timer just to make sure that the data was replaced correctly using DOM. After I launched the page, I made an update to the column in the database. The page kept displaying the same value from the first time the page was loaded. The column for the timer continued to change correctly. So, I believe that the recordset returned wasn't refreshed during all subsequent called to this function using the setInterval method. If I refreshed the page, I got the new value from the database. Does anyone has this experience and know how to fix it? See my sample code below:

In the Head section:
var call_work_timerID = 0;
function StartCallWorkTimer()
{
<%
    var adoConnection = Server.CreateObject("ADODB.Connection");
    adoConnection.Open("DSN=ic613; UID=oadba; PWD=oadba");
    var adoRecordset = Server.CreateObject("ADODB.Recordset");

    var QStr= "my query string that finds one matching record";
    adoRecordset.Open(QStr, adoConnection);
    if (adoRecordset.Eof == false && adoRecordset.Bof == false)
    { X= adoRecordset("idleavaildur").Value;
      Y= <my current time in milliseconds>;
    }
    <Use DOM to replace my X and Y fields in my table here>
    adoRecordset.Close();
    adoRecordset = null;
    adoConnection.Close();
    adoConnection = null;
}

In the Body:
<input type=button name=btnStop1 value="Start" onclick="javascript:call_work_timerID=setInterval( 'StartCallWorkTimer()',5000);">
<input type=button name=btnStop2 value="Stop" onclick="javascript:clearInterval(call_work_timerI D);">



 
Old April 10th, 2004, 02:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

in JavaScript:
Code:
setTimeout(DO_UR_JOB, 5000);
or simply
Code:
<meta http-equiv="refresh" content="5">
in HTML

Always:),
Hovik Melkomian.
 
Old April 10th, 2004, 12:23 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I see the <% and that tells you are writing ASP with javascript, correct?

Take a look at the source HTML for the page. I think you will not see the code that handles the database, but only the code that sets the dynamic values using the DOM. I imagine you are switching back from ASP (%>) and writing the DOM calls to update the page's text with the new values from the database with some mixure of javascript calls with ASP values (in <% %>) intertwined then switching back into the ASP code (<%). While this works, it's not doing what you want because the server-side hit to the database is only being made once and you are providing a static chunk of update code that is being called repeatedly by the browser using the interval timer. In order to do what you want using ASP you are going to have to refresh the whole page at the server.

Peter
-------------------------
Work smarter, not harder
 
Old April 10th, 2004, 12:32 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Didn't we cover this in two previous threads:

http://p2p.wrox.com/topic.asp?TOPIC_ID=11767
http://p2p.wrox.com/topic.asp?TOPIC_ID=11765

The same principle still applies: client side code runs at the client, server side code at the server. When the page first loads, the server side block runs, and the output is inserted between the { and the } of the StartCallWorkTimer mehtod. Then the page is sent to the browser where the method will run, using the same output from the server side code.

It looks like you're missing a few important client-server concepts....

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Bearsuit by Tindersticks (Track 11 from the album: Curtains)

 
Old April 10th, 2004, 11:09 PM
Authorized User
 
Join Date: Apr 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to pigtail Send a message via Yahoo to pigtail
Default

Hey, thanks a lot! I did forget that the server side code only gets executed once.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Refresh GridView Using Refresh Button msbsam ASP.NET 2.0 Professional 0 December 6th, 2006 05:57 AM
Does SetInterval spawn many threads? pigtail Javascript 0 April 10th, 2004 11:39 PM
Can I use setInterval to refresh recordset?? pigtail Javascript How-To 1 April 10th, 2004 08:12 AM
Refresh Recordset dhborchardt Classic ASP Databases 3 June 17th, 2003 12:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.