Hey guys, is there a more efficient way to do this?
setInterval('getAllWords()', 2000);
function getAllWords() {
$.ajax({
type: "GET",
url: "queries.php",
data: "action=all",
success: function(data){
dataArray = data.split('|');
$('#words').html('');
for (var i = 0; i < dataArray.length; i++) {
if ((dataArray[i]).length > 1) {
$('ul').append('<li>' + dataArray[i] + '</li>');
}
}
}
});
}
ELABORATION (?)
This basically connect every 2 seconds to the database and checks for updated words, is there an easy way to implement a push
request or something to minimise the database activity?
MORE INFO:
My hosting doesn't support installing of AJAX Push servers and they won't install one for me
听起来像您要服务器推送
If you can't install a push server, consider this hack:
Run an infinite loop that only sends an answer to your ajax request once there's something new (while(true) { check for updated words}).
In your ajax code you can then simply start a request and as soon as it's finished start a new one (eg. readyState == 4 --> start new request)
If you don't need internet explorer support, you could use webSockets
Otherwise there are several standard ways of implementing server push
Here is a way to implement long polling using php - use google to find more :)
If query caching is enabled on your db server, then this shouldn't be a cause for worry. The query cacher will not load the db until the words field is updated.
On other note,
I prefer using setTimeout than using setInterval. What if response from server takes more than 2 secs to return? setInterval will issue request again even if previous request is not complete.
So, I'd use this
function getAllWords() {
$.ajax({
type: "GET",
url: "queries.php",
data: "action=all",
success: function(data){
dataArray = data.split('|');
$('#words').html('');
for (var i = 0; i < dataArray.length; i++) {
if ((dataArray[i]).length > 1) {
$('ul').append('<li>' + dataArray[i] + '</li>');
}
}
setTimeout(getAllWords,2000);
}
});
}
getAllWords();
to minize the db activity, you could:
OR
you can use setInterval()
function, however efficiency of the method depend upon your requirement.
Please let me know your requirement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.