I have a website here and I would like users to be able to click links and or buttons inside of the chat windows but the page is auto refreshing at a very high rate using jQuery, what would be the most efficient way to allow this?
Here is my code for auto refreshing.
//Load the file containing the chat log
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "chatLogs/masterlog.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 500); //Reload file every 2.5 seconds
i would suggest you to implement a feature like when the user hold CTRL key pressed (or any other key) it stop the refreshing.
when the user release the key, it start again.
you could implement this simply like this :
//Load the file containing the chat log
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "chatLogs/masterlog.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
var i = setInterval (loadLog, 500); //Reload file every 2.5 seconds
document.onkeydown = function(e){
ctrl = e.ctrlKey;
if(ctrl)
clearInterval(i);
}
document.onkeydown = function(e){
ctrl = e.ctrlKey;
if(ctrl)
i = setInterval(loadLog, 500);
}
Related
I have infinite scroll and on top of this I have ( list of categories ) so when one of these buttons clicked the infinite scroll updated and new information loaded by ajax, after the data loaded the infinite scroll stop working
$(document).ready(function() {
// Infinite Ajax Scroll configuration
jQuery.ias({
container : '#events', // main container where data goes to append
item: '.items', // single items
pagination: '.event-nav', // page navigation
next: '.event-nav a', // next page selector
loader: '', // loading gif
triggerPageThreshold: 5 // show load more if scroll more than this
});
});
//Set up click filterBy event
$(document).on('click', '.filterBy' ,function (event) {
$( ".filterBy" ).removeClass('active');
$(this).addClass('active');
var url = '/rest/ajax/filter.json';
var divId = "event-category";
//Process button click event
loadAjax(this.id,url,divId);
});
//Set up click filterType event
$(document).on('click', '.filterType' ,function (event) {
$( ".filterType" ).removeClass('active');
$(this).addClass('active');
var url = '/rest/ajax/filterType.json';
var divId = "events";
//Process button click event
loadAjax(this.id,url,divId);
});
// JavaScript Document
function loadAjax(val,urlReq,divId)
{
$.ajax({
type: 'POST', // type of request either Get or Post
url: urlReq,// Url of the page where to post data and receive response
data: 'data='+val, // data to be post
beforeSend: function( xhr )
{
$( "#"+divId ).show();
$('html,body').animate({
scrollTop: $("#"+divId).offset().top
}, 1500);
$("#"+divId).html('loading.........');
},
success: function(data)
{
$("#"+divId).html(data);
} //function to be called on successful reply from server
});
}
I am trying to display a loading animation and hide the search input box before the ajax request has been sent, the ".se-pre-con" element is the loader.
As you can see below I am tring to show this in the beforeSend of the ajax request. However, when the user is typing in the search box then stops for the delay specified (1000ms) the input box still displays throughout the ajax request but it is unresponsive.
Then the results are returned, and the loading animation does the fadeOut so it the show() must be triggering but the page remains unchanged until the results are returned. I want to prevent the unresponsive page effect and show a loading animation during the request. Any help is much appreciated.
$(document).ready(function() {
var delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
$('#search_field').keyup(function() {
var target = $(this);
delay(function() {
getSearchResults(target.val());
}, 1000);
});
function getSearchResults(str) {
$.ajax({
beforeSend: function(){
$(".se-pre-con").show();
$("#search_field").hide();
},
url: "http://example.com/Search_Results.php",
dataType:"html",
data: {"search_term": str},
method: "post",
async: false,
success: function(data){
if(data !== null) {
$("#search_default").hide();
$("#search_results_wrapper").html(data);
}
$(".se-pre-con").fadeOut("slow");
$("#search_field").show();
$("#search_field").focus();
},
error: function(){
$(".se-pre-con").fadeOut("slow");
$("#search_field").show();
$("#search_field").focus();
}
});
}
});
I found the solution.
Solution
It started working when I removed the option:
async: false
Is there any SIMPLE way to create an autoscroll within a php/html chatbox?
I tried a few things, but they all interfered with other .js elements on the page and all end up not working.
to view live code of the chatbox click here!
heres what i have at the moment
// jQuery Document
$(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
//Load the file containing the chat log
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 2500); //Reload file every 2.5 seconds
//If user wants to end session
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the session?");
if(exit==true){window.location = 'index.php?logout=true';}
});
});
I suspect the weight you get your scroll height doesn't work. I have tried to type it in the command line of FF and didn't get an int nor an object.
Don't try to get the scroll height. Scroll further down... and more than that.
$("#chatbox").animate({ scrollTop: 99999 }, 'normal');
I have only tried with FF, but should work across most browsers.
Then, your success function is dead simple :
success: function(html){ $("#chatbox").animate({ scrollTop: 99999 }, 'normal');
$("#chatbox").html(html);
$("#chatbox").animate({ scrollTop: 99999 }, 'normal');
}
$(".msgln").last().offset().top
I have a code like this below :
$(document).ready(function() {
$("#content2").load("post_rf.php");
// set your initial interval to kick it off
var refreshInterval = setInterval(function() {
$("#content2").load('post_rf.php?randval='+ Math.random());
}, 1500);
// bind an event to mouseout of your DIV to kickstart the interval again
$("#content2").bind("mouseout", function() {
refreshInterval = setInterval(function() {
$("#content2").load('post_rf.php?randval='+ Math.random());
}, 1500);
});
// clear the interval on mouseover of your DIV to stop the refresh
$("#content2").bind("mouseover", function() {
clearInterval(refreshInterval);
});
$.ajaxSetup({
cache: false
});
});
What I want to do is when mouseover, data keep autorefresh.
In this case if I drag mouse into the area of mouseover, it stop auto refresh until I drag mouseout out the area of mouseover.
So is it possible to set onmouseover, data will keep auto refresh ?
i think what you want is the other way around,
$("#content2").bind("mouseover", function() {
refreshInterval = setInterval(function() {
$("#content2").load('post_rf.php?randval='+ Math.random());
}, 1500);
});
// clear the interval on mouseover of your DIV to stop the refresh
$("#content2").bind("mouseout", function() {
clearInterval(refreshInterval);
});
I'm a little confused about your question, well the following code is responsible for making stop the auto refresh when you mouseover
$("#content2").bind("mouseover", function() {
clearInterval(refreshInterval); // it's clearing the setInterval
});
If you just remove/disable the the line or whole function then it won't stop the auto refresh when you move your mouse on that element.
but if you only want to occur the auti refresh only on mouseover and again want to stop the auto refresh on mouseout then you can do as follows
$("#content2").bind("mouseover", function() {
refreshInterval = setInterval(function() {
$("#content2").load('post_rf.php?randval='+ Math.random());
}, 1500);
});
$("#content2").bind("mouseout", function() {
clearInterval(refreshInterval);
});
and should not use following code to start the auto refresh initially, just disable the code as follows
/*
var refreshInterval = setInterval(function() {
$("#content2").load('post_rf.php?randval='+ Math.random());
}, 1500);
*/
but you can keep the variable declarede as var refreshInterval=0;
I have been working on an ajax/php chat system where users can obviously chat with one another. Im concerned about server load, the way it was originally programmed was auto refresh div (chat box) every x seconds it only did this is the user was active as i timed out their inactivity. If they remained inactive for 10 minutes or so they would be shown as idle the system would then stop refreshing. Then i looked into Server-Sent Events with HTML5 which worked well however not all browsers work with this.
Does anyone have a better solution or is the div refresh ok for now? Hope someone can help thanks!
Consider using COMET, or take a look at Ajax Push Engine: Link
An example of a chat system using COMET: Link
// jQuery Document
$(document).ready(function(){
});
//jQuery Document
$(document).ready(function(){
//If user wants to end session
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the session?");
if(exit==true){window.location = 'index.php?logout=true';}
});
});
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
loadLog;
return false;
});
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
//Auto-scroll
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 1000);
</script>