I wrote a small jquery code to redirect to google.com after 2 seconds on pressing a button. The code works fine in Firefox, google chrome but not in the Internet Explorer.
I believe the location.assign function is not working since it is the one that is supposed to redirect to another url and all other functions like alert are working but not this one.
The code is as follows:
<script type="text/javascript">
$(document).ready(function(){
$(".prize_select").click(function(){
var timer = setInterval(redirect,2000);
function redirect()
{
location.assign("http://www.google.com");
timer = clearTimeout(timer);
}
});
});
</script>
I would also say that I have already tried these function:
location.assign
window.location
window.location.href
but none of them seems to work in Internet Explorer
You need setTimeout(), not setInterval(). Also, you need location.href, not just window.location. This script says, "When .prize_select is clicked, redirect to "cnn.com" after 2000 ms.
$(document).ready(function() {
$(".prize_select").click(function() {
window.setTimeout(function() {
location.href = 'http://www.cnn.com';
}, 2000);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button class="prize_select">Click me!</button>
Declare the function before using it. e.g.
var timer;
var redirect = function(timer) {
location.assign("http://www.google.com");
timer = clearTimeout(timer);
};
timer = setInterval(function() { redirect(timer); }, 2000);
Update: now passes timer instance
Related
I am trying to set a delay on the mousedown (click) which focuses the newWin.focus in code below:
So in short, the code below works fine. If the popup window is open, it re-focuses on it (bringing it back front) when the user clicks anywhere on the page.
I would like to have a 2 second delay when the user clicks on the page, THEN have it refocus on the new window.
I have spent hours trying to figure this out to no avail using various settimeout's here and there. So, I thought I would show what I have that works and hope someone might explain how my need can be accomplished.
PS: I am still learning my way around javascript and in NO WAY consider myself knowledgeable in the subject.
Thanks in advance!!
<script type="text/javascript">
var newWin;
function openPopup()
{
newWin=window.open('https://www.somesite/url.php','window','width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');
document.onmousedown=focusPopup;
document.onkeyup=focusPopup;
document.onmousemove=focusPopup;
}
function focusPopup(){
if(!newWin.closed){
newWin.focus();
}
}
</script>
You can add a setTimeout to your mousedown handler:
document.onmousedown = () => setTimeout(focusPopup, 2000);
<script type="text/javascript">
var newWin;
function openPopup()
{
newWin=window.open('https://www.somesite/url.php','window','width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');
document.onmousedown=()=>{
setTimeout(focusPopup,2000)
};
document.onkeyup=focusPopup;
document.onmousemove=focusPopup;
}
function focusPopup(){
if(!newWin.closed){
newWin.focus();
}
}
</script>
Just use setTimeout to add the delay.
The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
var newWin;
function openPopup() {
newWin = window.open('https://www.somesite/url.php', 'window', 'width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');
document.onmousedown = function() {
setTimeout(focusPopup, 2000);
};
document.onkeyup = focusPopup;
document.onmousemove = focusPopup;
}
function focusPopup() {
if (!newWin.closed) {
newWin.focus();
}
}
I have the below javascript code. Basically what I want to do is when a user is clicking on the hyperlink, I want to open a new tab, call https://somesite.com/logout, first, wait 5 seconds and then call https://www.somesite.com
This is possible to do? If so, what changes can I make to this code to get that done?
script type="text/javascript">
window.onload = function() {
document.getElementById("mySite").onclick = function() {myFunction()};
function myFunction() {
//your code goes here
var win1= window.open("https://somesite.com/logout");
win1.close();
}
};
</script>
<a href='https://www.somesite.com' target='_blank' id='mysite'>Click Here</a>
window.onload = function() {
document.getElementById("mysite").onclick = function() {myFunction()};
function myFunction() {
//your code goes here
var win1= window.open("https://somesite.com/logout");
win1.close();
window.setTimeout(function() {
window.open("https://somesite.com");
}, 5000);
}
};
getElementById is case sensitive, so be sure to use "mysite". You can set a timeout to open a window after 5000ms (5 seconds).
Also, be aware that browsers will typically block popups by default.
Just add window.setTimeout(window.location.reload(), 5000) after close()
I am using this javascript to auto refresh a page every 30 seconds.
<script type="text/javascript">
window.setTimeout(function(){ document.location.reload(true); }, 30000);
</script>
I need to give users an option to disable this function at any time before the page is reloaded.
Note: this is for an admin panel, not a public website, and the requirement from the client is that the page auto refreshes, with an option to stop the refresh before it happens by clicking a button.
Is there any way to achieve this... That is.. disable the javascript before it executes?
clearTimeout() : Cancels a timeout previously established by calling setTimeout().
You're searching for clearTimeout() :
var refresh = window.setTimeout(function(){ document.location.reload(true); }, 30000);
$('body').on('click', '#my-button', function(){
clearTimeout(refresh);
})
Hope this helps.
Do it this way:
var myVar;
function myFunction() {
myVar = window.setTimeout(function(){ document.location.reload(true); }, 30000);
}
function myStopFunction() {
clearTimeout(myVar);
}
You just have to call the myStopFunction in order to stop the auto reload.
Do the following:
reload = window.setTimeout(function(){ document.location.reload(true); }, 30000);
Or, if using jQuery,
$("#non-reload-button").click(function(){
clearTimeOut(reload)
});
Below is code that will continue to execute every 500ms until you click the button to stop it. Just replace the 500 with the time you want and replace the console.log with what ever operation you wish to run. Hope this helps!
let auto_refresh_active = true;
const refresh = () => {
window.setTimeout(e => {
console.log('Hey, I am running! Click the button to stop me!')
if(auto_refresh_active == true) refresh();
}, 500)
}
refresh();
document.querySelector('button').addEventListener('click', e => auto_refresh_active = false);
<button>stop running</button>
Problem: I have a asp.net button and on click of that I am displaying another window using window.open() at the client side using <script></script>
"I actually, need a popup (alert message) to be displayed on my parent page where my button is located once the user closes the child window."
Couple of things I tried are as follows:
I tried using setTimeOut() to have a time out for some milliseconds. This does not work as the control is not waiting until the time out is complete. It just proceeds to execute next set of code.
I tried using setInterval() but for some reason it is not working for me. Below is the code snippet of that:
$(document).ready(function () {
$('#<%=btnClick.ClientID%>').bind('click', function () {
var newWindow = window.open("http://www.google.com/", "google", 'resizable=1,width=900,height=800,scrollbars=1', '_blank');
newWindow.moveTo(0, 0);
var test = setInterval(function (e) {
if (newWindow.closed) {
alert("HEYY");
clearInterval(test);
__doPostBack("<%= btnClick.UniqueID %>", "");
}
else {
e.preventDefault();
}
}, 5000);
});
});
.
I also tried making an ajax call to open the new window and make it async : false, it again did not help me.
Bring your window and timer variable out of scope of the event handler. You need to do a polling i.e. periodically keep on checking if the windows has been closed. Using setInterval to do a polling will do the job.
var newWin, pollTimer;
$('#btnId').bind('click', function () {
newWin = window.open("...", "...", "");
pollTimer = window.setInterval(function() {
if (newWin.closed) {
window.clearInterval(pollTimer);
callCodeWhenPopupCloses();
}
}, 5000);
});
function callCodeWhenPopupCloses() {
alert("Popup closed.");
...
}
I'm wanting a button that when clicked will refresh the current page after a specified amount of time.
I currently have:
<script type="text/javascript">
setTimeout(function reload(){
location = ''
},1000)
</script>
<button onclick="reload()">Reload</button>
However, that JUST reloads the page without even having to click the button. I'm wanting the button to execute the script, and also a button to STOP the page reload.
This should be really simple but I can't figure it out :(
******EDIT**********
I'd also like the script to run on an infinite loop after the button is clicked.
Your setTimeout is called on page load. You need to put it inside the reload() function:
function reload() {
setTimeout(function() {
window.location.reload();
}, 1000);
}
To make the timer run every x seconds and reload only a part of the page you would need to use setInterval and an AJAX request, something like this:
var timer;
function reload() {
timer = setInterval(function() {
$.post('foo.html', function(data) {
$('#bar').html(data);
});
}, 1000);
}
function clear() {
clearInterval(timer);
}
This should do the trick
<script type="text/javascript">
function reload(){
setTimeout(function(){location.reload()}, 3000);
}
</script>
<button onclick="reload()">Reload</button>
What you wrote is
window.setTimeout("location = ''"; ,1000);
You were saying execute this function after 1 second. You need to define the setTimeout inside the function. Also there is a built in method to reload the page. Call that instead of setting the location to a blank string.
function reload() {
setTimeout( function() {
window.location.reload(true);
},1000);
}
Now to cancel the timeout, you need to use clearTimeout(timeoutId); You get the timeoutId from the integer that the setTimeout returns when you call it.
var timer = null;
function reload() {
timer = window.setTimeout( function() {
window.location.reload(true);
},1000);
}
function cancelReload() {
if (timer) {
window.clearTimeout(timer);
}
timer = null;
}
AND you said you want it to keep running. That will require cookies or localstorage.
var timer = null;
function reload() {
localStorage.reload = true; //set localstorage so we know to fire it off again
timer = window.setTimeout( function() {
window.location.reload(true);
},1000);
}
function cancelReload() {
if (timer) {
window.clearTimeout(timer);
}
timer = null;
localStorage.removeItem("reload"); //remove the key in localstorage
}
if (localstorage.reload) { //check localstorage to see if key is set
reload();
}
You need to wrap the whole thing in another function and call that from the button click