AJAX refresh!! where to put the timer? | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| |
Hello , i got this code that works great , now i want to have it refresh every 4 seconds , where can i put the timer to do that?
i realy need help!!
thanx!! | | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
i fixed it thanx1!!!! :)
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
Though I have an idea how you did, can you post how you solved it? It may help someone else who comes across this thread looking for a solution to a similar problem.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
The true :) ,it did solve one thing to refresh the content ,but it didnt solve the all thing i needed,
i just put the setTimeout in the right position ,and it did refresh , but it was doing refresh and bring me all the time to the first page!! (im using it with pagination from mysql database)
what i did: - function showPage(str) {
-
//Function that gets called
-
//Currently we only call one other sub, but this could change
-
showStates(str)
-
setTimeout('showPage("1")', 4000);
-
}
-
i hope it will help some one!!
and maybe some one can help me to solve it to stay in the next page with refreshing also!!
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
That's because you're always calling it with "1". Use an integer and update it each time you make the call. Note that you can use setInterval in place of setTimeout.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
this is the 2 files im using ,maybe some one can take a look at them and tell me what im doing wrong! or what to do!!
the first file is the ajax script to call the second file ,the second file is calling the ddatabase for the records ,my problem is with the pagination ,if i put setTimeout to refresh the page it's returning back to the first page after the amount of time i put in the setTime out!!
what can i do to keep refreshing the current page im in to!!
thanx
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
When you mention "current page", is it a paginated view, e.g. the URL has a page number as a parameter?
Note that your onload is incorrect. It should be something like: - window.onload = function() {
-
setInterval(showPage,4000);
-
}
You could have the page number as a global variable which can be incremented in showPage.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
it is pagination ,i put the onload you told me ,but when im going to page No 2 for example ,it's jumpijng back to page 1 ,after 4 seconds!
im not so good in php and javascript :)
i try so many combinations and it keep jumping to page one!!
thanx for youe help!
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
OK, on the first page when you're on page 2, what's the full page URL including after the ?
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
this is the problem:
window.onload = showPage("1");
the 1 is a connection for the +str that is reloading the page no 1
when it's like that window.onload = showPage("1"); ,i can navigate with the links ,but i want to have it refresh also if there is new data!!
this the url with all: for content
var url="getuser.php?bid_id=<?=$bid_id;?>"
url=url+"&p="+str
url=url+"&t=con"
url=url+"&sid="+Math.random()
this the url with all: for navigation
var url="getuser.php?bid_id=<?=$bid_id;?>"
url=url+"&p="+str
url=url+"&t=nav"
url=url+"&sid="+Math.random()
i removed the onload you wrote me , so now its without it
this is the link for example how it is now ,without settimeout or iterval http://www.bonker.co.il/product_detail.php?bid_id=205 |  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
In your link, there's no page number, so how would it be reflected if you were on page 2? In other words, how do you get onto the second page? Is there a link on the page, or do you want this to happen automatically after 4 seconds?
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
look at the bottom of the results ,its there
5,4,3,2,1 in red
i want the current page im in to be auto refresh after 4 seconds.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
Oh, I see. These links are to showPage with the correct page number.
What you can do is add a global variable, pageno which is set to 1 initially. Then in the links, set this variable to the corresponding number. In the meantime, showPage will be called every 4 seconds using setInterval.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
if i knew how to do it ,it was good then !
im not this good in JS or PHP :)
:(
thanx alot!!
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
You only need to make a few changes.
1. Change window.onload to what I posted earlier.
2. Change showPage() to accept 0 parameters: function showPage() {
3. Add a global variable pageno which will be used in place of that parameter:
4. Replace str in showPage() with pageno.
5. Change the links to set pageno instead of calling showPage: - <a href="#" onclick="pageno = 2; return false;">2</a>
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
thanx alot man!! , when i get home im going to try it!!!!
thanks!!!
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
i try everything and i cannot figure it out how to do it :(
any help will be appreciated !!
thanx
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
I can't see any changes in the page linked to earlier. What changes did you make? Have you got a link with the changes?
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
i did so meny combinations!
i dont know how to change the var from the tha JS.
the JS is calling the php file with the str that is equal to 1
the navigation numbers are in the php file so i think i cannot change the links!
if i could chang the JS from the php file to change the str to different number then i think it will be fine!
but i dont know how to cahnge the JS from the php file!!
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
here are both of the files!
the java script |  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer? Quote:
Originally Posted by canabatz i dont know how to change the var from the tha JS.
the JS is calling the php file with the str that is equal to 1
the navigation numbers are in the php file so i think i cannot change the links!
if i could chang the JS from the php file to change the str to different number then i think it will be fine! You don't need to change the variable from PHP. You need to adjust your JavaScript. You can also change the links too.
First things first, add a variable that will hold the current page number (point no. 3). Then use that in place of str, i.e. no need to pass a parameter to showPage(). Worry about the links later.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
ok ,this is what i did and it is working for the corrent page!
i changed all the str with 'pageno' ,the pageno = 1
if i change the pageno to 2 then it will stay in page 2 and do the settimeout
here is the code for the moment: - var pageno = 1;
-
function showPage(pageno) {
-
//Function that gets called
-
//Currently we only call one other sub, but this could change
-
showStates(pageno)
-
-
}
-
-
-
function showStates(pageno) {
-
//This sub will populate a table with all the states and get the
-
//pagination built
-
-
-
//Make the AJAX connection for both the navigation and content
-
xmlHttp=GetXmlHttpObject()
-
xmlHttp2=GetXmlHttpObject()
-
-
//If we cant do the request error out
-
if (xmlHttp==null || xmlHttp2==null ) {
-
alert ("Browser does not support HTTP Request")
-
return
-
}
-
-
-
//First build the navigation panel
-
var url="getuser.php?bid_id=<?=$bid_id;?>"
-
url=url+"&p="+pageno
-
url=url+"&t=nav"
-
url=url+"&sid="+Math.random()
-
-
//Once the page finished loading put it into the div
-
xmlHttp2.onreadystatechange=navDone
-
-
//Get the php page
-
xmlHttp2.open("GET",url,true)
-
xmlHttp2.send(null)
-
-
//Build the url to call
-
//Pass variables through the url
-
var url="getuser.php?bid_id=<?=$bid_id;?>"
-
url=url+"&p="+pageno
-
url=url+"&t=con"
-
url=url+"&sid="+Math.random()
-
-
//Once the page finished loading put it into the div
-
xmlHttp.onreadystatechange=stateChanged
-
-
//Get the php page
-
xmlHttp.open("GET",url,true)
-
xmlHttp.send(null)
-
-
setTimeout('showPage(pageno)', 4000);
-
}
-
-
function navDone() {
-
//IF this is getting called when the page is done loading then fill the pagination div
-
if (xmlHttp2.readyState==4 ) {
-
//Update the Div tag with the outputted text
-
document.getElementById("pgNavigation").innerHTML=xmlHttp2.responseText
-
}
-
}
-
-
function stateChanged() {
-
//IF this is getting called when the page is done loading the states then output the div
-
if (xmlHttp.readyState==4 ) {
-
//Update the Div tag with the outputted text
-
document.getElementById("pgContent").innerHTML=xmlHttp.responseText
-
-
}
-
}
-
-
function GetXmlHttpObject() {
-
//Determine what browser we are on and make a httprequest connection for ajax
-
var xmlHttp=null;
-
-
try {
-
// Firefox, Opera 8.0+, Safari
-
xmlHttp=new XMLHttpRequest();
-
}
-
catch (e) {
-
//Internet Explorer
-
try {
-
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
-
}
-
catch (e) {
-
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
}
-
-
return xmlHttp;
-
}
-
-
-
-
//Onload start the user off on page one
-
window.onload = showPage(pageno);
how can i change the pageno with the links?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
Progress- good! Since it's to be continuous at intervals, I would recommend setInterval over setTimeout. Note that they're used slightly differently. The setInterval only needs to be called once and it will happily continue on its merry way. The onload would be the best place for that. Note, though, that the way you're currently using onload is incorrect. It needs to be assigned to a function, not the result of a function (see the suggestion I posted earlier).
One other thing before we move onto the links. In showPage if you want to use the global variable set to 1, you need to remove pageno in between the brackets.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
if i live the showpage empty ,then the links dont work!
what i change now is - var pageno = 1;
-
function showPage() {
-
//Function that gets called
-
//Currently we only call one other sub, but this could change
-
showStates(pageno)
-
-
}
-
-
window.onload = function() {
-
setTimeout(showPage(),4000);
-
}
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
listen every thing is working as i want it!
the only think i need is to fix the pageno = 1 to be dynamic
like if i press the second page the pageno will have +1
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer? Quote:
Originally Posted by canabatz - window.onload = function() {
-
setTimeout(showPage(),4000);
-
}
That would call it after 4 seconds, rather than immediately. Is that what you want?
For the links, just set them as I posted earlier: - <a href="#" onclick="pageno = 2; return false;">2</a>
-
<a href="#" onclick="pageno = 3; return false;">3</a>
and so on.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
the problem is that there is no links ,the link is creted dynamicly, look at the php file ,if i had href links that could be easier!!
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
In post #20 where you've posted the PHP code, look at line number 98. That's where you need to change the link to set pageno to $page.
| | Familiar Sight | | Join Date: Oct 2008
Posts: 141
| | | re: AJAX refresh!! where to put the timer?
thanx alot man!!
your last post got me to where i wanted!!!
you are king :)
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: AJAX refresh!! where to put the timer?
You're welcome. Glad to hear that you got it working :) Post again to the forum if you have more questions.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,439 network members.
|