I'm having problem on auto refreshing a content in a web page
up vote
2
down vote
favorite
I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.
setInterval(function() {
$("#status").load('refresh.php');
},1000);
In my javascript, I'm automatically refreshing the #status with refresh.php every second.
But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.
Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?
javascript php ajax database
add a comment |
up vote
2
down vote
favorite
I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.
setInterval(function() {
$("#status").load('refresh.php');
},1000);
In my javascript, I'm automatically refreshing the #status with refresh.php every second.
But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.
Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?
javascript php ajax database
1
What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.
setInterval(function() {
$("#status").load('refresh.php');
},1000);
In my javascript, I'm automatically refreshing the #status with refresh.php every second.
But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.
Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?
javascript php ajax database
I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.
setInterval(function() {
$("#status").load('refresh.php');
},1000);
In my javascript, I'm automatically refreshing the #status with refresh.php every second.
But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.
Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?
javascript php ajax database
javascript php ajax database
asked Nov 21 at 13:15
Gerald Manibale
347
347
1
What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40
add a comment |
1
What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40
1
1
What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40
What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript
add a comment |
up vote
0
down vote
This code will be request refresh.php
later at timeouts.min
every time when new (requested) value of #status
is equal to current. If values are different, next timeout will be timeouts.min
again.
var timeouts={
min:1000, // min await timeout = 1s
max:16000 // max await timeout = 16s
};
var currentTo = timeouts.min;
function setAutoRefresh( to ){
setTimeout( function() {
var current = $("#status").text();// or .html()
$("#status").load('refresh.php', function() {
// if we are load a new value
if( current !== $("#status").text()) {
currentTo = timeouts.min;
// if loaded value are same to current
} else {
currentTo += timeouts.min; // or currentTo *= 2; for example
if( currentTo > timeouts.max ){
currentTo = timeouts.max;
}
}
console.log('Next #status refresh after ' + currentTo + 'ms');
// Set a new timeout
setAutoRefresh( currentTo );
});
}, to);
}
// Set the first timeout
setAutoRefresh( timeouts.min );
this would also spamresfresh.php
which i think the OP wants to avoid.
– Mojo Allmighty
Nov 21 at 13:31
No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
– Николай Лубышев
Nov 21 at 13:33
1
load()
is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
– charlietfl
Nov 21 at 13:46
load() is asynchronous
Thanx! Fixed.
– Николай Лубышев
Nov 21 at 13:49
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript
add a comment |
up vote
1
down vote
You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript
add a comment |
up vote
1
down vote
up vote
1
down vote
You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript
You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript
answered Nov 21 at 13:28
Pamicel
686
686
add a comment |
add a comment |
up vote
0
down vote
This code will be request refresh.php
later at timeouts.min
every time when new (requested) value of #status
is equal to current. If values are different, next timeout will be timeouts.min
again.
var timeouts={
min:1000, // min await timeout = 1s
max:16000 // max await timeout = 16s
};
var currentTo = timeouts.min;
function setAutoRefresh( to ){
setTimeout( function() {
var current = $("#status").text();// or .html()
$("#status").load('refresh.php', function() {
// if we are load a new value
if( current !== $("#status").text()) {
currentTo = timeouts.min;
// if loaded value are same to current
} else {
currentTo += timeouts.min; // or currentTo *= 2; for example
if( currentTo > timeouts.max ){
currentTo = timeouts.max;
}
}
console.log('Next #status refresh after ' + currentTo + 'ms');
// Set a new timeout
setAutoRefresh( currentTo );
});
}, to);
}
// Set the first timeout
setAutoRefresh( timeouts.min );
this would also spamresfresh.php
which i think the OP wants to avoid.
– Mojo Allmighty
Nov 21 at 13:31
No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
– Николай Лубышев
Nov 21 at 13:33
1
load()
is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
– charlietfl
Nov 21 at 13:46
load() is asynchronous
Thanx! Fixed.
– Николай Лубышев
Nov 21 at 13:49
add a comment |
up vote
0
down vote
This code will be request refresh.php
later at timeouts.min
every time when new (requested) value of #status
is equal to current. If values are different, next timeout will be timeouts.min
again.
var timeouts={
min:1000, // min await timeout = 1s
max:16000 // max await timeout = 16s
};
var currentTo = timeouts.min;
function setAutoRefresh( to ){
setTimeout( function() {
var current = $("#status").text();// or .html()
$("#status").load('refresh.php', function() {
// if we are load a new value
if( current !== $("#status").text()) {
currentTo = timeouts.min;
// if loaded value are same to current
} else {
currentTo += timeouts.min; // or currentTo *= 2; for example
if( currentTo > timeouts.max ){
currentTo = timeouts.max;
}
}
console.log('Next #status refresh after ' + currentTo + 'ms');
// Set a new timeout
setAutoRefresh( currentTo );
});
}, to);
}
// Set the first timeout
setAutoRefresh( timeouts.min );
this would also spamresfresh.php
which i think the OP wants to avoid.
– Mojo Allmighty
Nov 21 at 13:31
No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
– Николай Лубышев
Nov 21 at 13:33
1
load()
is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
– charlietfl
Nov 21 at 13:46
load() is asynchronous
Thanx! Fixed.
– Николай Лубышев
Nov 21 at 13:49
add a comment |
up vote
0
down vote
up vote
0
down vote
This code will be request refresh.php
later at timeouts.min
every time when new (requested) value of #status
is equal to current. If values are different, next timeout will be timeouts.min
again.
var timeouts={
min:1000, // min await timeout = 1s
max:16000 // max await timeout = 16s
};
var currentTo = timeouts.min;
function setAutoRefresh( to ){
setTimeout( function() {
var current = $("#status").text();// or .html()
$("#status").load('refresh.php', function() {
// if we are load a new value
if( current !== $("#status").text()) {
currentTo = timeouts.min;
// if loaded value are same to current
} else {
currentTo += timeouts.min; // or currentTo *= 2; for example
if( currentTo > timeouts.max ){
currentTo = timeouts.max;
}
}
console.log('Next #status refresh after ' + currentTo + 'ms');
// Set a new timeout
setAutoRefresh( currentTo );
});
}, to);
}
// Set the first timeout
setAutoRefresh( timeouts.min );
This code will be request refresh.php
later at timeouts.min
every time when new (requested) value of #status
is equal to current. If values are different, next timeout will be timeouts.min
again.
var timeouts={
min:1000, // min await timeout = 1s
max:16000 // max await timeout = 16s
};
var currentTo = timeouts.min;
function setAutoRefresh( to ){
setTimeout( function() {
var current = $("#status").text();// or .html()
$("#status").load('refresh.php', function() {
// if we are load a new value
if( current !== $("#status").text()) {
currentTo = timeouts.min;
// if loaded value are same to current
} else {
currentTo += timeouts.min; // or currentTo *= 2; for example
if( currentTo > timeouts.max ){
currentTo = timeouts.max;
}
}
console.log('Next #status refresh after ' + currentTo + 'ms');
// Set a new timeout
setAutoRefresh( currentTo );
});
}, to);
}
// Set the first timeout
setAutoRefresh( timeouts.min );
edited Nov 21 at 14:22
answered Nov 21 at 13:30
Николай Лубышев
40337
40337
this would also spamresfresh.php
which i think the OP wants to avoid.
– Mojo Allmighty
Nov 21 at 13:31
No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
– Николай Лубышев
Nov 21 at 13:33
1
load()
is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
– charlietfl
Nov 21 at 13:46
load() is asynchronous
Thanx! Fixed.
– Николай Лубышев
Nov 21 at 13:49
add a comment |
this would also spamresfresh.php
which i think the OP wants to avoid.
– Mojo Allmighty
Nov 21 at 13:31
No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
– Николай Лубышев
Nov 21 at 13:33
1
load()
is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
– charlietfl
Nov 21 at 13:46
load() is asynchronous
Thanx! Fixed.
– Николай Лубышев
Nov 21 at 13:49
this would also spam
resfresh.php
which i think the OP wants to avoid.– Mojo Allmighty
Nov 21 at 13:31
this would also spam
resfresh.php
which i think the OP wants to avoid.– Mojo Allmighty
Nov 21 at 13:31
No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
– Николай Лубышев
Nov 21 at 13:33
No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
– Николай Лубышев
Nov 21 at 13:33
1
1
load()
is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues– charlietfl
Nov 21 at 13:46
load()
is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues– charlietfl
Nov 21 at 13:46
load() is asynchronous
Thanx! Fixed.– Николай Лубышев
Nov 21 at 13:49
load() is asynchronous
Thanx! Fixed.– Николай Лубышев
Nov 21 at 13:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412882%2fim-having-problem-on-auto-refreshing-a-content-in-a-web-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40