Is it possible to remove/disable internal JavaScript on a page after 'X' minutes so that it does not work at all after 'X' minutes of time?
Try like below,
<script type="text/javascript" id="hideAfter">
// your code here...
// it will removes the script tag after 10 sec...
setTimeout(function () {
var ele = document.getElementById("hideAfter");
ele.parentNode.removeChild(ele);
}, 10000);
</script>
Related
My table was blinking in every 5 second because of this auto refresh script in my php page. here is my code for auto refresh
<script type='text/javascript'>
$(document).ready(function (){
var table = $('#autorefresh');
// refresh every 5 seconds
var refresher = setInterval(function(){
table.load("utility_autorefresh.php");
}, 5000);
setTimeout(function() {
clearInterval(refresher);
}, 5000);
});
</script>
instead of using .load(), you can try using $.get() or $.post() to get the new content, then use .html() to put it into the table.
var refresher = setInterval(function(){
$.post("utility_autorefresh.php",function(dom) { table.html(dom); });
}, 5000);
let see if it working and not blinking anymore.
I have a jquery mobile phonegap application. I would like to execute a function every 30 seconds the user stays on a particular page.
If the user stays on a particular page say page1 , i would like to execute a function every 30 seconds,
To put it in simpler words
If active page is page1
fire getmessages() every 30 seconds.
How do i achieve this
USE settimeinterval
Check this DEMO
<div id="div2>
<input type="text" name="divText" value="q3"/>
</div>
setInterval(function() {
alert('HI')
}, 30000);
Time is in ms (1000= 1 sec)
If you're using jQuery Mobile 1.4, you need to listen to pagecontainershow and pagecontainerhide events in order to execute functions with interval based on page id.
Retrieve page's id on those events then use switch / case to execute functions as well as clearInterval when page is hidden.
/* setInterval function's name */
var interval;
/* 1) On page show, retrieve page's ID and run checkPage()
2) On page hide, clearInterval() */
$(document).on("pagecontainershow", function () {
var activePage = $.mobile.pageContainer.pagecontainer("getActivePage")[0].id;
checkPage(activePage);
}).on("pagecontainerhide", function () {
clearInterval(interval);
});
/* Run function(s) based on page's ID */
function checkPage(page) {
switch (page) {
case "p1":
interval = setInterval(function () {
/* function(s) */
}, 30000);
break;
case "p2":
interval = setInterval(function () {
/* function(s) */
}, 30000);
break;
}
}
Demo
You can setInterval.
Code:
$(document).ready(function(){
function myFunction()
{
setInterval(function(){alert("Hello")},3000);
}
$('#click').click(function(){
myFunction()
})
})
HTML:
<p>Click the button to wait 3 seconds, then alert "Hello".</p>
<p>After clicking away the alert box, an new alert box will appear in 3 seconds. This goes on forever...</p>
<button id="click">Try it</button>
Replace 3000(3 sec) --> 30000 (30 sec) for your requirement.
Demo link http://jsfiddle.net/dhana36/2c4ps/
Working Fiddle
Reference Link
setInterval(function() {
// Do something every 30 seconds
}, 30000);
so im a beginner in php & javascript who wants to make a div contains an ads script in it to refresh every minute .
i thought i wrote everything right in my code :
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load').load('reload.html').fadeIn("slow");
}, 60000); // refresh every 60000 milliseconds
</script>
<div id="load"></div>
and this is the reload file :
<html>
<script src="http://www.[random-website].com/getad.php?46151;80391;300x250"></script>
</html>
when the div reloads nothing happens , what the probleme ?
Its because a div with that id doesnt exist in that moment. Try this:
<script type="text/javascript">
$(document).ready(function(){
var auto_refresh = setInterval(
function ()
{
$('#load').load('reload.html').fadeIn("slow");
}, 60000); // refresh every 60000 milliseconds
});
</script>
You can also move your script block after <div id="load"></div>
You might try to update your jquery version. Ver 1.3 is obsolete.
I'm struggling to get an animated gif to run in IE. Works in all other browsers, but in IE it just freezes. I've researched this and looks like a delay using setTimeout might work. Not too sure how I add this to the following function:
<script type="text/javascript">
$(function(){
$('#photo_form').on("submit", function () {
$('#loading').show();
});
});
</script>
The gif is inside a div called 'loading' which is hidden. Would I add the timeout to onClick of the button or within the function itself?
Why does IE make things so difficult!?
Any help with solving this problem would be very helpful.
You mean something like this?
$(function() {
$('#photo_form').on("submit", function () {
setTimeout(function () {
$('#loading').show();
}, 100);
});
});
Try this curde, untested psedo-code:
var startTime;
var thread;
$(function(){
$('#photo_form').on("submit", function () {
$('#loading').show();
startTime = time();
thread = setInterval("showLoadingGif", 1);
});
function showLoadingGif() {
var timeToWait = 5; //change interval as needed
if(timeToWait + startTime <= currentTime) {
//show the gif
clearInterval(thread);
}
}
It's been a long time since I've worked with javascript, so this almost certainly needs adjustment; but the principle is the same: keep calling that function, and let that function decide when to stop being called.
setTimeout() will cause your page to freeze while you wait. setInterval will run your code asyncronously.
I have placed the following script in the head tag of my asp.net master page.
What this script is suppose to do re-direct to the Timeout.aspx page after specific inactive time has elapsed.
If the user scrolls / clicks on the page, then the time is reset. On running the page I get an error:
Jscript engine runtime error: object expected.
Code:
var wintimeout;
function SetWinTimeout() {
wintimeout = window.setTimeout("window.location.href='../Timeout.aspx';",
60000); //after 5 mins i.e. 5 * 60 * 1000
}
$('body').click(function () {
window.clearTimeout(wintimeout);
//when user clicks remove timeout and reset it
SetWinTimeout();
});
window.onload = SetWinTimeout;
You need to change the call of setTimeOut
Change
function SetWinTimeout() {
wintimeout = window.setTimeout("window.location.href='../Timeout.aspx';",
60000); //after 5 mins i.e. 5 * 60 * 1000
}
To
function SetWinTimeout() {
wintimeout = window.setTimeout(function(){
window.location.href='../Timeout.aspx';
}, 60000);
}
If this is not an error because JQuery is not loaded, try the following to run the whole script after the page (and JQuery for that matter) has been loaded:
$(function()
{
$('body').click(function () {
window.clearTimeout(wintimeout);
//when user clicks remove timeout and reset it
SetWinTimeout();
});
SetWinTimeout();
});