I have the following code:
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var task = ss.getRange(2,4).getValue(); //ss.getRange("A2").getValue();
var who = Session.getActiveUser();
var date = new Date();
function start() {
var last = ss.getLastRow();
ss.getRange(last+1, 1).setValue(task);
ss.getRange(last+1, 2).setValue(who);
ss.getRange(last+1, 3).setValue(date);
}
function end() {
var last = ss.getLastRow();
ss.getRange(last, 4).setValue(date);
ss.getRange(1,1).setValue(date);
ss.getRange(1,2).setValue(ss.getRange(last+1, 3));
}
in a Google Sheet script. It works on desktop - basically what it does is it will track when a user starts and stops a given task.
The sheet shows up fine in the Sheets app, but the script doesn't run. What needs to happen so that it will run? Is there some way to run the programs on the app?
I somehow cannot get the Disqus this.callbacks.onNewComment to work. What could be wrong? Im trying to alert('hey!') once a new comment is posted. Source
<script>
var disqus_config = function () {
this.page.url = PAGE_URL;
this.page.identifier = PAGE_IDENTIFIER;
this.callbacks.onNewComment = [function(comment) {
alert(comment.id);
alert(comment.text);
alert('hey!');
}];
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://example.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
PAGE_URL and PAGE_IDENTIFIER need to be specific to your site
This callback is for detecting a new post by this browser window. Not new posts arriving from other users (or even your own user in another browser windows)
That 2nd one threw me as I thought it was to indicate new post had arrived
Let's say it's June 18 and a person runs the javascript and it runs a function on load but it doesn't run the function later that day even if the person closes the window or program in my case.
However, on June 19 if the person opens the program and it runs the javascript then it runs the function because it's a new day and hasn't run that day yet...
So basically, how do I make an onload function only work once per a day?
It has to be javascript because the program I'm using can only be assisted by javascript files and nothing else.
The person has to download the files to their computer so how would I go about saving and retrieving the localStorage?
You can use localStorage to achieve this. Sample Code:
/* returns null if local storage is not defined */
var localVal = localStorage.getItem('someUniqueName');
if(localVal == null){
// execute the function
}else{
var tempd = new Date();
var str = tempd.getDay() + tempd.getMonth() + tempd.getFullYear();
if(localVal.localeCompare(str) == -1){
//execute function
localStorage.setItem('someUniqueName',str);
}
}
If you wish to create a cookie and handle it, then you may go through this question
You can create a cookie on the user's browser that persists till the end of the day. If you are using jQuery, you can do as follows ( taken from here ):
var currentDate = new Date();
expirationDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()+1, 0, 0, 0);
$.cookie("ultOS", "5", {expires: expirationDate});
If you are using plain javascript, then the way to do this would be as follows (from here):
function createCookie(name,value,date) {
if (date) {
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
var today = Date();
var midnight = today.getDate()+1
midnight.setHours(0,0,0,0);
createCookie("Valid","true",midnight);
At the beginning of every page load, just check the value of this cookie.
In my app I'm displaying 10 charts (charts are from dygraphs.) to monitor data. For displaying charts I'm getting data from my sever by sending ajax request to 4 servlets on every 5 seconds. After 10-15 mins (don't know exact time.) my browser crashes saying "aw!! snap." What could be the reason? Is it javascript that is causing it? or is it because I'm sending request every 5 seconds?
Browser tested: Firefox and Chorme.
Note:- When I refresh the browser after crash it again works fine for 10-15 mins.
JS code:
var i=0;
var loc = new String();
var conn = new String();
var heapUsage = new String();
var cpuUsage = new String();
var thrdCnt = new String();
var heapUsageConsole = new String();
var cpuUsageConsole = new String();
var thrdCntConsole = new String();
var user = new String();
var MemTotal = new String();
function jubking(){
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "MonitorDBServlet";
xmlhttp.open("POST", url, false);
xmlhttp.send(null);
var str = xmlhttp.responseText;
var strArr = str.split(",");
url = "MonitorTomcatServlet";
xmlhttp.open("POST", url, false);
xmlhttp.send(null);
var appstr = xmlhttp.responseText;
var appArr = appstr.split(",");
url = "MonitorConsoleTomcatServlet";
xmlhttp.open("POST", url, false);
xmlhttp.send(null);
var appstrConsole = xmlhttp.responseText;
var appArrConsole = appstrConsole.split(",");
url = "CpuMemoryServlet";
xmlhttp.open("POST", url, false);
xmlhttp.send(null);
var statesStr = xmlhttp.responseText;
var states = statesStr.split(",");
if(i>30){
loc = loc.substring(loc.indexOf("\n")+1);
loc += i+","+strArr[0]+","+strArr[1]+"\n";
//--- Do same thing all other var
} else {
loc += i+","+strArr[0]+","+strArr[1]+"\n";
//--- Do same thing all other var
}
document.getElementById("dbSize").innerHTML = strArr[3];
document.getElementById("HeapMemoryUsageMax").innerHTML = appArr[1];
document.getElementById("HeapMemoryUsageMaxConsole").innerHTML = appArrConsole[1];
g = new Dygraph(document.getElementById("dbLocks"),
",locksheld,lockswait\n"+loc+"");
g = new Dygraph(document.getElementById("activeConnection"),
",Connections\n"+conn+"");
g = new Dygraph(document.getElementById("example2"),
",heapUsage\n"+heapUsage+"");
g = new Dygraph(document.getElementById("example3"),
",cpuUsage\n"+cpuUsage+"");
g = new Dygraph(document.getElementById("example4"),
",thread,peakThread\n"+thrdCnt+"");
g = new Dygraph(document.getElementById("example6"),
",heapUsage\n"+heapUsageConsole+"");
g = new Dygraph(document.getElementById("example7"),
",\n"+cpuUsageConsole+"");
g = new Dygraph(document.getElementById("example8"),
",thread,peakThread\n"+thrdCntConsole+"");
g = new Dygraph(document.getElementById("cpuStates"),
",user,system,nice,idle\n"+user+"");
g = new Dygraph(document.getElementById("memStates"),
",MT,MF,B,C,ST,SF\n"+MemTotal+"");
i = i + 1;
setTimeout("jubking()", 5000);
}
You can use about:crashes in FF to view the specific reason for your crash. As mentioned by others, you could be leaking memory if you're caching off data (assigning it to a variable) returned by your AJAX call and not clearing it when the next call is made.
Edit:
Just saw your comment - 1,923,481 K is definitely too much - you're leaking data somewhere. What OS are you running? If you run FF from console in *nix, you usually get some form of a dump into console when something's going wrong (not sure about Windows).
You could possibly try decreasing your poll intervals to once every few seconds and step through the script using Firebug or Chrome's debugger to see what's happening. Worst case, start commenting things out until you figure out exactly what is making your app crash. And then, figure out a way to fix it :)
I suspect that your dygraphs usage is, as you note in your comments, the source of your trouble. It looks like you're binding new graphs over and over again when you only want to update the data, using a moving window for the data would also help. Try reworking your updater to work like this pseudo-JavaScript:
var graphs = {
dbLocks: {
graph: new DyGraph(/* ... */),
data: [ ]
},
activeConnection: {
graph: new DyGraph(/* ... */),
data: [ ]
},
// etc.
};
var DATA_WINDOW_SIZE = 1000; // Or whatever works for you.
function update(which, new_data) {
var g = graphs[which];
g.data.push(new_data);
if(g.data.length > DATA_WINDOW_SIZE)
g.data.shift();
g.graph.updateOptions({ file: g.data });
}
function jubking() {
// Launch all your AJAX calls and bind a callback to each
// one. The success callback would call the update() function
// above to update the graph and manage the data window.
// Wait for all the above asynchronous AJAX calls to finish and
// then restart the timer for the next round.
setTimeout(jubking, 5000);
}
The basic idea is to use window on your data with a reasonable maximum width so that the data doesn't grow to chew up all your memory. As you add a new data point at the end of your data cache, you drop old ones off the other end once you hit your maximum comfortable size.
You can find some techniques for waiting for several asynchronous AJAX calls to finish over here: How to confirm when more than one AJAX call has completed? (disclosure: yes, that's one of my other answers).
The answer above advocates re-using your Dygraph object and calling g.updateOptions({file:...}) to reduce memory usage. This is a great way to do it.
The other way is to call g.destroy() before you redefine the Dygraph object. This will make dygraphs clear out all of its internal arrays and DOM references. Example:
g = new Dygraph(...);
g.destroy();
g = new Dygraph(...);
Read more here: http://blog.dygraphs.com/2012/01/preventing-dygraphs-memory-leaks.html
var startTime = new Date().getTime();
var myWin = window.open("http://www.hizlial.com/hediyelik/hediyelik-urunler/zippo-jack-daniels-ltr-flask-hediye-seti_16.004.4126.1233.htm","_blank")
window.onload = function() { opener.window.endtime = new Date().getTime(); }
var endTime = opener.window.endtime;
var timeTaken = endTime-startTime;
document.write(timeTaken);
i edited the new code but this time i couldnt write the timeTaken value? so whats wrong here?
That's because javascript isn't a threaded language, meaning that it doesn't wait for things to happen, it will open the page and while the page is loading will go on and do other stuff. What you would need to do is to add this into the target page:
window.onload = function() { window.opener.endtime = new Date().getTime(); }
Or as ramazan murat wrote:
var startTime = new Date().getTime(); var myWin = window.open("www.mozilla.com");
window.onload = function() { opener.window.endtime = new Date().getTime(); };
var timeTaken = endTime-startTime;
Another way to go about this would be to do this:
<script>start = new Date().getTime();</script>
<iframe src="http://www.mozilla.com/" onload="end=new Date().getTime();"></iframe>
I would use Net in Firebug to test a page's load time.
Use Google Chrome's developer tool's timeline function.
If you don't need to do this programmatically, then Firebug's Net tab will give you all the information you need to know about how long given resources on a page take to request.