Onmouseover data keep autorefresh - php

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;

Related

jQuery .load stops video embedded in page / reloads whole page

I've a question regarding reloading a div with jQuery-.load. Code:
<h1>Official Live-Stream</h1>
<object width="560" height="315" data="http://www.dailymotion.com/embed/video/x10sbxw"></object>
</br>
</br>
<script>
(function($)
{
$(document).ready(function()
{
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#content').hide();
$('#loading').show();
},
complete: function() {
$('#loading').hide();
$('#content').show();
},
success: function() {
$('#loading').hide();
$('#content').show();
}
});
var $container = $("#liveracingblog");
$container.load("../live/addon/addon_ticker.php");
var refreshId = setInterval(function()
{
$container.load('../live/addon/addon_ticker.php');
}, 30000);
});
})(jQuery);
</script>
<div id="liveracingblog"></div>
The problem I have, is that every 30 seconds not only the liveracingblog-div reloads, but the whole page, which makes the video embedded in line #2 stop.
You can observe the problem here: http://www.racingblog.de/racingbloglive/ (the code is part of a Wordpress page template, but I don't think that matters)
How can this be fixed?
I looked at the page and the #content div appears to include the embedded video, which you are hiding and showing in the js. You want to only hide and show the #liveracingblog div
The following change should work:
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#liveracingblog').hide();
$('#loading').show();
},
complete: function() {
$('#loading').hide();
$('#liveracingblog').show();
},
success: function() {
$('#loading').hide();
$('#liveracingblog').show();
}
});
Also I notice that a huge amount of records are being pulled out of the database every 30 seconds, and because you are using the load() method, any user scrolling down will be returned to the top of this div. I would suggest using forgetting about hiding the divs at all. Instead, replace load() with a $get to return any new records (since the last request), then prepend these new records to the #liveracingblog div.
Either way, good luck with that, and let me know if you want more clarification on this. Adrian

Trouble with dynamic refreshing div

I make div which refresh when file is updated. But it continuously refresh (fade out and fade in every second).I't source test2.php
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js>
</script>
<script>
$(document).ready(function() {
$('#loaddiv').load('check.chat.php');
});
var auto_refresh = setInterval( function() {
$.ajax(
{
type: 'POST',
data:"id=100",
url: "check.chat.php",
success: function(result)
{
if($("#loaddiv").html() != result)
{
$("#loaddiv").fadeOut("fast")
$("#loaddiv").html(result);
$("#loaddiv").fadeIn("slow");
}
}
});
}, 1000);
</script>
<div id="loaddiv"></div>
And file on site: **
Who knows what's the problem?
This part:
$("#loaddiv").fadeOut("fast")
$("#loaddiv").html(result);
$("#loaddiv").fadeIn("slow");
Should be:
$("#loaddiv").fadeOut("fast", function(){
$("#loaddiv").html(result);
$("#loaddiv").fadeIn("slow");
});
In your case, both fades are called at the same time, making an animation queue, causing it to go from one phase to another in about the same time the interval triggers again.
UPDATE
To see logs, do this: console.log("html: ", $("#loaddiv").html(), "result: ", result);

how to combine setInterval with mouseenter?

I am trying to make it so a div refreshes with ajax. The code itself is implemented and working already. I want the div to refresh every 30 seconds but only on an active tab. From what I understand setInterval will refresh every time regardless of whether the tab is in use or not. Id like to combine a mouseenter (or some other kind of user interaction that implies the user is active on the site) with setInterval so that the setInterval doesnt get fired if inactive.
currently I have this code which works well on the initial page load. There is no refresh during the first 30 seconds, nor is there a refresh until mouseenter on the div. However after the initial 30 seconds it refreshes on every mouseenter.
$(document).ready(function(){
$("#container").hide();
$("#loading").show();
$("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });
function refresh() {
$("#refresh").mouseenter(function() {
// $("#container").hide();
$("#loading").show();
$("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });
});
clearInterval(intervalID);
}
var intervalID = setInterval(refresh, 30000); // Will alert every 30 second.
// clearInterval(intervalID); // Will clear the timer.
});
Just set the interval when the mouse cursor is in the tab you want, and clear it when it's outside:
var intervalID, lastRefresh = 0;
$("#refresh").mouseenter(function() {
var diff = new Date().getTime() - lastRefresh;
intervalID = setTimeout(function() {
refresh();
intervalID = setInterval(refresh, 30000);
}, 30000 - diff);
}).mouseleave(function() {
clearInterval(intervalID);
});
function refresh() {
$("#loading").show();
$("div#refresh").load('example.com/load.php',
function(){ $("#container").show(); $("#loading").hide(); });
lastRefresh = new Date().getTime();
}
Now the <div> is refreshed in the instant the mouse enters inside its borders, and every 30 seconds from that moment. This stops when the mouse leaves the <div>.
If the mouse enters again, it checks for the last time the refresh function was called. If less than 30 seconds have passed, it waits until 30 seconds pass.
Pro-tip: clearInterval also clears timed events generated by setTimeout, just like clearTimeout cancels setInterval.
Try this:
$(document).ready(function(){
var intervalID;
$("#container").hide();
$("#loading").show();
$("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });
function refresh() {
// $("#container").hide();
$("#loading").show();
$("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });
}
$("#refresh").mouseenter(function() {
intervalID= setInterval(refresh, 30000);
});
$("#refresh").mouseleave(function() {
clearInterval(intervalID);
});
$(function(){
var intervalID;
$("#container").hide();
refresh();
function refresh() {
$("#loading").show();
$("div#refresh").load('example.com/load.php', function(){
$("#container").show();
$("#loading").hide();
});
}
$("#refresh").on({
mouseenter: function() {
intervalID = setInterval(refresh, 30000);
},
mouseleave: function() {
clearInterval(intervalID);
}
});
});
​

How to keep the block text in div tag refresh function?

I have a page that the function is to refresh div tag.
Div tag function is refresh the data will receive.
So far that's ok.
When I block the text using mouse, that's will clear the block text based on timing "20000". This below the JS script function.
<script src='js/jquery.min.js'></script>
<script>
$(document).ready(function()
{
$("#content2").load("post_rf.php");
var refreshId = setInterval(function()
{
$("#content2").load('post_rf.php?randval='+ Math.random());
}, 20000);
$.ajaxSetup({ cache: false });
});
</script>
What I want to do is, how to keep the block text in div refresh function ?
Because some user maybe want to copy the text. In this case, user must quickly copy the text before div refresh.
Maybe the example like facebook post live update.
You want to assign your interval to a variable, when user mouse overs your DIV then use clearInterval, when mouse out setInterval again.
var interval;
$(div).bind("mouseout", function() {
interval = setInterval(refresh, 1000);
});
$(div).bind("mouseover", function() {
clearInterval(interval);
});
EDIT
Sorry I posted that on a phone and it's hard to write code that way, try this:
<script src='js/jquery.min.js'></script>
<script>
$(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());
}, 20000);
// 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());
}, 20000);
});
// clear the interval on mouseover of your DIV to stop the refresh
$("#content2").bind("mouseover", function() {
clearInterval(refreshInterval);
});
$.ajaxSetup({ cache: false });
});
</script>

Don't refresh when typing

This is my code:
// Refresh feeds, but ONLY when you are NOT typing
var refreshId = setInterval(function() {
$("#load-feeds").load('/actions/loadfeed.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
I'm working on a kind of "wall" like Facebook where you can comment on each other's posts. The wall automatically refreshes with AJAX (every 9 seconds), but when you're typing and the textfield is focused it removes the focus and the content of the textbox after the page refreshed.
How can I make it only refresh when you're NOT typing and make it stop when you're typing. Thanks! I've looked everywhere and tried everything, but no luck.
I suppose you have an input box like this:
<input type="text" id="input-box" ... />
All you have to do is mantain a global variable than says if this input has the focus:
var isTyping = false;
$("#input-box").focus(function() {
isTyping = true;
});
$("#input-box").blur(function() {
isTyping = false;
});
then you just have to check this variable before to allow the update:
// Refresh feeds, but ONLY when you are NOT typing
var refreshId = setInterval(function() {
if (!isTyping) {
$("#load-feeds").load('/actions/loadfeed.php?randval='+ Math.random());
}, 9000);
}
$.ajaxSetup({ cache: false });
You should keep a timeout timer running when they type, and check before you load:
var typing = false,
theTimer,
refreshId = setInterval(function() {
typing || $("#load-feeds").load('/actions/loadfeed.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
$('#theInput').keyup(function() {
typing = true;
theTimer && clearTimeout(theTimer);
theTimer = setTimeout(function() {
typing = true;
}, 500);
});

Categories