Javascript,auto refresh - javascript

i currently am using the below javascript code below,
<script type="text/javascript">
function PrintWindow()
{
window.print();
CheckWindowState();
}
function CheckWindowState()
{
if(document.readyState=="complete")
{
window.close();
}
else
{
setTimeout("CheckWindowState()", 2000)
}
}
PrintWindow();
//window.onfocus = function() { window.close(); }
// Trying to auto refresh the page after 3 seconds of no use.
function setIdle(cb, seconds) {
var timer;
var interval = seconds * 1000;
function refresh() {
clearInterval(timer);
timer = setTimeout(cb, interval);
};
$(document).on('keypress, click', refresh);
refresh();
}
setIdle(function() {
location.href = location.href;
}, 3);
</script>
My original question was how to go home when the print box was finished but was told this was impossible, so i am now wondering how to auto refresh the page after so many seconds have passed, i have added in code to do this but it is not working
i have now managed to fix it, if any one is wondering :
var timer = null;
function goAway() {
clearTimeout(timer);
timer = setTimeout(function() {
window.location = 'http://localhost:8080/fileuploadWithPreview';
}, 5);
}
window.addEventListener('mousemove', goAway, true);
goAway(); // start the first timer off
This when the window is closed takes me to the home page so it is possible to do

Related

reload iframe and click button inside iframe as much as I want

window.onhashchange = function () {
window.setTimeout(function () {
let frame = document.querySelector("body > iframe");
if (frame !== null) {frame.replaceWith(frame);}
}, 1000);
}
//reload the iframe
var i = 0;
while (i < 100) {
async function DStuff(){
await document.querySelector("#Btn").click()
}
setTimeout(() => { DStuff() }, 3000)
window.onhashchange();
i++;
}
//click button
I want to refresh iframe and click button as much as I want. When I run this script it doesn't loop. Also the script should wait for a second after click the button.
Drop the loop and simply use a setInterval:
window.onhashchange = function () {
window.setTimeout(function () {
let frame = document.querySelector("body > iframe");
if (frame !== null) {frame.replaceWith(frame);}
}, 1000);
}
//reload the iframe
var i = 0;
let myVar = setInterval(function(){
i++;
document.querySelector("#Btn").click();
window.onhashchange();
if (i === 100) {
clearInterval(myVar);
}
},3000);
The above waits for 3 seconds until it clicks again while it stops after reaching 100 clicks.
Needless to say, the iFrame must be on the same domain, otherwise it won't be accessible due to cross-site restriction.
If you want to use a different time delay between the reload and the click, you can use two functions that recursively call each other, with a count to stop the loop:
var totalIterations = 0;
function clickButton() {
console.log("clickButton called")
// Your Click-button code here
//document.querySelector("#Btn").click()
// In 1 second, call the reload frame function
setTimeout(reloadFrame, 1000)
}
function reloadFrame() {
console.log("reloadFrame called")
// Your Reload-frame code here:
//let frame = document.querySelector("body > iframe");
//if (frame !== null) { frame.replaceWith(frame); }
// If we haven't run 100 times yet, run the click button in 3 seconds
if (totalIterations < 100) {
setTimeout(clickButton, 3000)
totalIterations++
}
}
// Call the function to start
clickButton()

Disable timer within setInterval function with dynamic parameters

I wanted to pass dynamic parameters into a setInterval function (see question here) and specifically #tvanfosson's comment.
But now, I also want to disable that timer if a certain condition is met. I tried to define the timer variable as a global variable but I still get the timer as a undefined on this line:
console.log('else. timer=' + timer);:
else. timer=undefined
<script language="javascript" type="text/javascript">
var timer;
var params={};
params.color='light';
$(document).ready(function () {
timer=createInterval(showSmallWidget, params.color, 500);
});
function createInterval(f, dynamicParameter, interval) {
setInterval(function () {
f(dynamicParameter);
}, interval);
}
function showSmallWidget(color) {
if ($('#widget').html() == '') {
//do stuff
}
else {
console.log('else. timer=' + timer);
if (timer) { console.log('CLEAR TIMER'); timer.clearInterval(); timer = null; }
}
}
</script>
I tried to create a JSFiddle, but I can't get it to work properly: https://jsfiddle.net/puhw3z2k/
There are a couple problems:
1) You have to return the timerID from your createInterval() function:
function createInterval(f, dynamicParameter, interval) {
return setInterval(function () {
f(dynamicParameter);
}, interval);
}
2) clearInterval() works like this clearInterval(timer), not timer.clearInterval().

How can I stop custom jQuery function which is running?

I have a custom jQuery function. When it runs every 5 seconds.
(function($) {
$.fn.mycustomfunction = function() {
interval = setInterval(function() {
console.log("I am running every 5 seconds");
}, 5000);
}
return this;
};
})(jQuery);
$("#container").mycustomfunction();
I have a
clearInterval(interval);
to stop, but I also want to stop the function completely. How can I do that ?
Functions you add to this object will be attached to your object and Simple and naive solution will follow:
(function($) {
$.fn.mycustomfunction = function() {
interval = setInterval(function() {
console.log("I am running every 5 seconds");
}, 1000);
this.stop= function(){
clearInterval(interval);
}
// another function
this.alert = function(msg){
alert(msg)
}
return this;
};
})(jQuery);
to stop use
var feature = $("#container").mycustomfunction();
feature.stop();

Javascript auto page refresh code

this is the code that comes in head section and it will automatically refresh the whole page in 1 min as i put 6000 in the code below
<script type="text/javascript">
setTimeout('window.location.href=window.location.href;', 6000);
</script>
is there any way for example, when there's 10 seconds left to refresh the page then, a button will display and say "Click here to reset timer" and it will reset that timer to 1 min again?
<script language="javascript">
var timeout,interval
var threshold = 15000;
var secondsleft=threshold;
startschedule();
window.onload = function()
{
startschedule();
}
function startChecking()
{
secondsleft-=1000;
if(secondsleft <= 10000)
{
document.getElementById("clickme").style.display="";
document.getElementById("timercounter").innerHTML = Math.abs((secondsleft/1000))+" secs";
}
}
function startschedule()
{
clearInterval(timeout);
clearInterval(interval);
timeout = setTimeout('window.location.href=window.location.href;', threshold);
secondsleft=threshold;
interval = setInterval(function()
{
startChecking();
},1000)
}
function resetTimer()
{
startschedule();
document.getElementById("clickme").style.display="none";
document.getElementById("timercounter").innerHTML="";
}
</script>
Please wait...<span id="timercounter"></span>
<button id="clickme" style="display:none;" onclick="javascript:resetTimer();">Click here to reset timer</button>
Assuming you have the following html for the button:
<button id="cancel-reload-button" style="display: none" onclick="cancelReload()">Cancel Reload</button>
And this as the script (Note: this gives the idea, but is not neccesarily fully tested):
// Variable for holding the reference to the current timeout
var myTimeout;
// Starts the reload, called when the page is loaded.
function startReload() {
myTimeout = setTimeout(function() {
document.getElementByID("cancel-reload-button").style.display = "inline";
myTimeout = setTimeout(function() {
window.location.reload();
} 10000)
}, 50000);
}
// Cancel the reload and start it over. Called when the button is
// clicked.
function cancelReload() {
clearTimeout(myTimeout)
startReload()
}
// On page load call this function top begin.
startReload();
I created two functions, one for starting the reload and the second one for cancelling it.
Then I assigned the timeout to the variable myTimeout which can be used to later cancel the timeout.
Then I called myTimeout twice - Once for 50 secs, at which point it shows the button and once for 10 secs after which it finally reloads.
How about below? If you click on OK to reset timer, it would keep giving the confirm box every 50 seconds. If you click cancel, it will refresh the page in 10 seconds.
setInterval(function(){ var r = confirm("Reset Timer");
if (r == true) {
setTimeout('window.location.href=window.location.href;', 60000);
} else {
setTimeout('window.location.href=window.location.href;', 10000);
}
}, 50000);
Note: In your question you specified 1 minute, but your code works for 6 seconds(6000 -- > 6 seconds not 60 seconds) I have included for a minute
You can use 2 setTimeout calls, one to make the "Reset" button show up and another one for the refresh timer reset. The trick is to store the second setTimeout on a global variable and use clearTimeout to reset it if the button is pressed.
Here is some JavaScript code to illustrate:
<script type="text/javascript">
var autoRefreshTime = 30 * 1000; // 60000ms = 60secs = 1 min
var warningTime = autoRefreshTime - (10 * 1000); // 10 secs before autoRefreshTime
waitTimeout = setTimeout('window.location.href=window.location.href;', autoRefreshTime);
warningTimeout = setTimeout('ShowResetButton();', warningTime);
function ShowResetButton() {
// Code to make the "Reset" button show up
}
// Make this function your button's onClick handler
function ResetAutoRefreshTimer() {
clearTimeout(waitTimeout);
waitTimeout = setTimeout('window.location.href=window.location.href;', autoRefreshTime);
}
</script>
The way I would do it is make a function with a timeout, and invoke that function
<script type="text/javascript">
var refreshFunc = function(){
setTimeout(function(){
var r = confirm("Do you want to reset the timer?");
if(r === false){
window.location.href=window.location.href;
}else{
refreshFunc();
}
}, 6000);
};
refreshFunc();
</script>
One big problem with using confirm in this case is you cannot program it to reject. You would have to implement you own modal/dialog box so you can auto reject in 10 seconds.
Try using setInterval():
var time;
$(function() {
time = $('#time');
$('#reset').on('click', reset);
start();
});
var timer, left;
var start = function() {
left = +(time.text()); //parsing
timer = setInterval(function() {
if (0 <= left) {
time.text(left--);
} else {
clearInterval(timer);
location.replace(location);
}
}, 1000);
};
var reset = function() {
if (timer) {
clearInterval(timer);
}
time.text('59');
start();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h1><span id='time'>59</span> second(s) left</h1>
<input id='reset' value='Reset' type='button' />

Javascript Inactivity timer logic

I am trying to implement a inactivity timer using javascript
user should be shown a confirm "You are inactive for 2 min, pls click yes to continue"
If the user does not respond for 1 min the user will be redirected to some page
<script type="text/javascript">
var firstTimer = 0;
var SecondTimer = 0;
function closewindow() {
window.setInterval(ShowAlert, 1000);
}
function ShowAlert()
{
firstTimer++;
SecondTimer++;
if(firstTimer==2)
{
firstTimer=0;
confirm('Do you want to Continue');
}
}
function FinalClose()
{
window.setInterval(ShowFinalAlert, 1000);
}
function ShowFinalAlert()
{
if(SecondTimer==3)
{
window.location.href="http://www.google.com";
}
}
</script>
<body onload="closewindow();FinalClose();">
The problem with the above code is if the user does not respond to the first alert, the second timer is not firing, that is when the overall count is 3 min he should be redirected.
what is wrong with the above code.
I'll take a swing at this one
var idle_timer = 120000; // check every 2 minutes
var kick_timer = 60000; // kick user after 1 minute
var redirect = function(){
window.location = "http://google.com";
};
var idler = function(){
// start the kick timer
var kick_timer = setTimeout(redirect, kick_timer);
// prompt user to click OK
if (confirm("You get the boot in 60 seconds. Do you want the boot?")) {
clearTimeout(kick_timer);
}
else {
redirect();
}
};
//schedule idler
setInterval(idler, idle_timer);

Categories

Resources