I have been using this code with IE for sometime to reload a page after 5 seconds of no activity. I cant figure out why it stopped working in chrome.
<script language="JavaScript" type="text/javascript">
<!--
if (document.layers) {
window.captureEvent(onmousemove);
}
document.onmousemove = oreset
var tID = '';
function oreset(){
clearTimeout(tID)
count=0
reloadPage()
}
function reloadPage(){
count++
if(count==5){ // 5 seconds
window.location="/sd/clockin/testclockin.php";
}
tID = setTimeout("reloadPage()",1000);
}
//-->
</script>
I can't tell you why that doesn't work in Chrome, but here's a script that is kind've better overall:
var expirer=setTimeout(expireFunc,5000);
function expireFunc(){
window.location.replace('/sd/clockin/testclockin.php');//or use .href instead if you don't like replace
}
//work around the random chrome bug here
var lastX=0,lastY=0;
window.addEventListener('mousemove',function(e){
//more workaround stuff
if(e.clientX===lastX&&e.clientY===lastY)
return;
lastX=e.clientX;
lastY=e.clientY;
console.log('moved');
clearTimeout(expirer);
expirer=setTimeout(expireFunc,5000);
},false);
I also had to workaround the bug I explained in the comments. http://jsfiddle.net/7cT9U/ is a working example.
Related
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
The following piece of code autosaves a page but it also times it out by loging out and taking the user to a time out page. How can i chnage it so that it only does the auto save part but not the time out?
<script language='javascript'>
function Save() {
var hdnSessionTimeOut = document.getElementById('fast22_MainCtn_fast44');
hdnSessionTimeOut.value = 'SessionTimeOut';
__doPostBack('Fast22$MainCtn$btnSave', '');
}
function Redirect() {
window.location = "SessionTimeout.aspx"
}
window.onload = function () {
if ('True' == 'True') setTimeout(Save, 30000);
else setTimeout(Redirect, 30000);
}
</script>
I tried reducing it to the following but and I think it worked but it changed the doc to view mode instead of edit mode. and you have to click edit again. Also when it in edit mode, the counter still works and it gives and error. Is there a way to have it auto save and then go back again to edit mode?
<script language='javascript'>
function Save() {
__doPostBack('ctl00$MainContentPlaceHolder$btnSave', '');
}
window.onload = function () {
if ('True' == 'True') setTimeout(Save, 10000);
else setTimeout(Save, 25000);
}
</script>
I've written a function which is loading correctly (see the hokey alert at the top) which goes on to call SP.SOD.executeFunc to check if the device is an ipad then call SetFullScreenMode(true);
window.onload = function () {
alert('has loaded');
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', goToFullScreen);
function goToFullScreen() {
alert('never gets called on an iPad'); // doesn't matter if isiPad is correct, this should at least happen
var isiPad = navigator.userAgent.indexOf('iPad') != -1;
if(isiPad) {
SetFullScreenMode(true);
}
}
};
It works just fine in Chrome and IE8 but doesn't work in iPad so executeFunc has any specific browser requirements. The "has loaded" message appears but nothing else executes.
Any suggestions?
Seems like the problem with the script not executing was my fault, I needed to prompt the script to notify pending functions. Not clear why this doesn't work on the iPad specifically, but there you go.
For info, the script that works is;
window.onload = function () {
SP.SOD.executeOrDelayUntilScriptLoaded(goToFullScreen, 'sp.js');
function goToFullScreen() {
var isiPad = navigator.userAgent.indexOf('iPad') != -1;
if(isiPad) {
SetFullScreenMode(true);
}
}
SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("sp.js");
};
I've written a function to catch a loop after it has run over a specified time.
var t1 = new Date().getTime();
while(true){
//general code in here...
document.body.appendChild(document.createElement('div')); //This causes the problem
if(isInfinite(t1,3000)){
alert('Loop stopped after 3 seconds')
break;
}
}
function isInfinite(t1,timeLimit){
var t2 = new Date().getTime();
if(t2-t1> timeLimit){
return true;
}
else return false;
}
It works as expected, but when I try to append nodes to the document it fails to catch in chrome & safari.
What's weird is it works when I have the debugger running, and it works in FF. What is causing this?
Maybe you are firing this JS code before DOM is ready. Here is my implementation.
<!--Basically I am firing this script with onload event function.-->
<html>
<head></head>
<body onload="init()">
<script>
function init() {
alert("asg");
var t1 = new Date().getTime();
while(true) {
//general code in here...
document.body.appendChild(document.createElement('div')); //This causes the problem
if(isInfinite(t1, 3000)) {
alert('Loop stopped after 3 seconds')
break;
}
}
function isInfinite(t1, timeLimit) {
var t2 = new Date().getTime();
if(t2 - t1 > timeLimit) {
return truite;
} else return false;
}
}
</script>
</body>
</html>
However, this script is most likely going to crash depending on how browser manufacturers have set their restriction on looping (This usually occurs when main thread is blocked). So I think it is much better idea to use setTimeout function for this example.
Just for your interest
Javascript is event based and it has only 1 thread. You have mentioned that you are going to show it to beginners. Why not explain them event based nature of JS instead of doing it this way?
I am seeing a major memory leak within Firefox and IE on my below code. To be fair, it could very well be my poor implementation and it needs changing, to allow Firefox and other browsers to garbage collect.
Does anyone have any suggestions on how to tweak the code to allow for a more efficient way of refreshing the page?
<input type="checkbox" onclick="sel1()" id="AutoRefresh">
<script type="text/javascript">
function sel1(){
var ref = document.getElementById('AutoRefresh').checked;
if(ref == true) {
setInterval(function(){
document.getElementById('dataRefreshButton').click(); }, 2000);
}
window.alert("Auto refresh on");
}
</script>
I think this will be better.
<script type="text/javascript">
function getdata(){
// get your data
}
var intGetData;
function sel1(){
var ref = document.getElementById('AutoRefresh').checked;
if(ref == true) {
intGetData = setInterval(getdata, 2000);
window.alert("Auto refresh on");
}
else{
clearInterval(intGetData);
window.alert("Auto refresh off");
}
}
</script>
Change setInterval to setTimeout. Your current code will set up a new interval to click the element every 2s when clicked. If you click once, you trigger an avalanche.
Or even better, you stop the interval:
var handle = null;
function sel1(){
var ref = document.getElementById('AutoRefresh').checked;
clearInterval(handle);
if (ref)
handle = setInterval(function(){
document.getElementById('dataRefreshButton').click();
}, 2000);
window.alert("Auto refresh on");
}