For example: In my Google Spreadsheets document there is a timer with a start and stop button. So far everything works. But if someone clicks on the 'stop' button by mistake although the timer is currently not running, it will causes issues and let the timer display a large and incorrect time. So I'm searching for methods to gray out the 'stop' button until 'start' was pressed. After, 'start' should be grayed out and 'stop' now available.
Has anybody ideas?
And then there's that I'm a total beginner in terms of scripting so the best bet would be if someone could help me out with the the script code.
Best method to prevent a script to run in parallel is to use the Lock Service.
When your button is pressed, you run a method that will acquire a lock and release it when finished. If you happen to click the button while previous task is still running, the lock acquire will fail and you can for example show an alert or just ignore the error.
function onButtonClick(){
const lock = LockService.getDocumentLock()
try{
lock.waitLock(1000)
// do some work
} catch(e){
if(e.message.indexOf('Lock')>=0){
SpreadsheetApp.getUi().alert(e.message)
}
} finally{
lock.releaseLock()
}
}
There is a method in Class Sheet call getDrawings() which returns all of the drawings on a sheet and that reveals the Class Drawing which has methods that allow you to reset the macro so you could set it to a dummy macro that doesn't do anything which should in effect disable the button.
Related
I have made this script for my forum and I was wondering if anyone could guide me how to get the browser to play an audio file if the user hovers over the class style113, but it has to give them a warning alert that says "audio is about to play" if they press ok on the alert it should play if they press cancel it should not. How can I achieve this? Here is the script I have so far:
my script has been removed
You can use the following code:
var elems = document.getElementsByClassName("style113");
for(i in elems) {
elems[i].addEventListener("mouseover", function() {
if(confirm(" %%% CONFIRMATION MESSAGE %%% ")) {
// %%% CODE TO PLAY SOUND %%%
}
});
}
What it does:
Loops over elements of class style113
Adds an event listener to each element to event mouseover
In each event listener, creates a confirm() popup (has two buttons, one to confirm and one to cancel)
If the confirm() method returns true (if the positive button is clicked), then play sound
Working example on JSFiddle
UPDATE As per OP request in the comments below, you can add this code to your specific code in the for loop:
document.getElementsByClassName('style113')[x].addEventListener("mouseover", function() {
if(confirm("audio is about to play")) {
// %%% CODE TO PLAY SOUND %%%
}
});
I'd also advise you to clean up your source code with better indenting practices. Also, avoid making too many DOM requests (e.g., repetitive document.getElementsByClassName()) and look instead to caching DOM requests.
I'm new to JavaScript.
I'm writing a simple code to add it in a chrome extension call it Shortkeys.
I just want the code to make a simple action:
Wait for a click of the mouse and then click a button in certain positions of the screen after 500 ms...
This is what I have written until this moment but is not working:
document.addEventListener('click', TimerThenPlay);
function TimerThenPlay(e) {
setTimeout(500)
document.elementFromPoint(1175, 85).click();
stop(TimerThenPlay);
clearTimeout(TimerThenPlay);
return;
};
What I'm doing wrong?
EDIT:
I have an APP running on Chrome...
I need to Click a Link and wait 500 ms to click a button... i can do that manually but sometimes dsnt work and i have to try again..
I realize that chrome has an extension that you can inject to the page a javascript code when u press a key in your keyboard. Thats why im using Shorkeys (if u know a better extension for this, just tell me).
Well... i have assign the < key to run the code...
What i need, is that everytime that i hit the < key... Chrome waits for a click (so i have time to look for the link that i want to open with de button)...
And when i click the link, it waits 500 ms and then click the button in the position that i select ( i cant use the Button ID cause it changes every minute).
I have tried the codes above and it works for the first time.. then, i dnt know why is keeping clicking 500 ms after a make a mouse click in the next pages... How can i stop that loop in the next page?
function TimerThenPlay(e) {
setTimeout(function(){
document.elementFromPoint(1175, 85).click();
stop(TimerThenPlay);
clearTimeout(TimerThenPlay);
},500)
}
SetTimeout method takes two arguments. First is function to execute after second argument time passes.
Hope this helps
Your setTimeout syntax is wrong.
Syntax of setTimeout:
setTimeout(function(){}, time)
you need to update your setTimeout function
function TimerThenPlay(e) {
setTimeout(function(){
document.elementFromPoint(1175, 85).click();
stop(TimerThenPlay);
clearTimeout(TimerThenPlay);
return;
},500)
};
I am trying to alter the code given here: http://jsbin.com/iFesOvOs/1/edit in such a way that a single button would both start and stop the timer.
If the timer is stopped, it will get started n press of the button and if it is running, it will be stopped.
I need to do this because I am making a mobile site with lot of features and we do not have space to place an additional button.
What I have tried:
I have tried altering some attribute of the button, so that we can detect if the timer is running or not and take action as per that.
I have tried storing the timer's state in localStorage, fetch it back up and take action as per that.
I have tried keeping a global variable, but that too didn't work
Sometimes, the desired function didn't get executed at all (with no error messages) and sometimes, I got the error message of variables going out of scope - which I tried to rectify by use of global variables but only in vain.
What's wrong here, can someone help me out?
This works...
<input type="button" value="start" onclick="start(this);">
function start(b) {
if (b.value == 'stop'){
b.value = 'start'
clearInterval(clocktimer);
return
}
b.value = 'stop'
clocktimer = setInterval("update()", 1);
x.start();
}
You can add class with name (start) to your button, add function that check if the class button name is start then start the trimer and change the button class name to stop, else when the class name is stop then stop the timer and change the class name to start.
I have a page where I show a throbber when I navigate away from the page. Like <a onclick="throbber.show()"> on every link. When I now navigate back in Firefox, the throbber is still shown.
Is there any javascript event that is fired to the arriving webpage when I click back? Or one that is fired just when the webpage is changed to the new one? Or can I make my throbber more intelligent?
Thanks for any input!
put this in your html:
<form name="_browser"><input id="checker" value="1" type="hidden"></form>
and also this javascript:
function cacheCheck()
{
var checker = document.getElementById("checker");
if (checker.value == 2) return true;
checker.value = 2;
checker.defaultValue = 2;
return false;
}
function cacheReload()
{
if (cacheCheck()) location.reload(true);
}
and then call cacheReload when your page loads:
<body onload="cacheReload()">
Dldnh's answer inpired me to do some tests. I suspected that the body.onload() event would be called when going back and forth. So I created a simple testpage and found out that this is true in Firefox 10, IE7, IE 8, IE 9 and Chrome 17. Also jQuery(document).ready() will be called.
The very simple solution for hidind the throbber would therefore be either using
<body onload="hideThrobber()">
or using jQuery ready
jQuery(document).ready(function () {
hideThrobber();
};
to hide the throbber then. I implemented this and it seems to work fine on my page. Would be great if somebody with a similar problem could confirm this.
I also found this interesting Stackoverflow question. While it is a little outdated, the point that calling javascript on navigation back and forth slowing down the page is still true. But I would guess that todays JS-Engines are fast enough so this is not a real issue anymore.
If you can't turn off the throbber from the page you navigate to, there are a few things you can do. The trick is that the page will be left active, so you can start up some things before you leave, in the onclick. They aren't perfect though.
Start a timer. The timer will be running when you return to the page, so the timeout routine will be called, and you can switch the throbber off there.
Problem: if you set the timer interval too small, the timeout routine will be called before the user has actually left the page, and the throbber will stop. Or if you set the interval too large, it will take a while before the timeout routine kicks in after they have returned.
Add an event listener to the body that responds to the mousemove event, so that as soon as the user moves the mouse, the routine that turnes off the throbber will be called.
Problem: if the user clicks the browser's Back button, the mouse will be outside the window when the page is redisplayed, so the throbber will remain visible until the user moves the mouse into the window.
So, take your pick. Or do both. Just remember to clean up afterwards - stop the timer, remove the event listener.
I have a Flex application where I want to give the user a warning if they hit the back-button so they don't mistakenly leave the app. I know this can't be done entirely in Actionscript due to cross-browser incompatibility. What I'm looking for is just the Javascript implementation to catch the back-button.
Does anyone have a simple non-library cross-browser script to catch the back-button? I can't seem to find a post that shows an example.
You can use the window.onbeforeunload event.
window.onbeforeunload = function () {
return "Are you sure you want to leave my glorious Flex app?"
}
The user can press okay to leave, cancel to stay.
As you stated, this throws the alert any time the page changes. In order to make sure it only happens on a back button click, we have to eliminate the alert message whenever they're leaving the page from natural, expected sources.
var okayToLeave = false;
window.onbeforeunload = function () {
if (!okayToLeave) {
return "Are you sure you want to leave my glorious Flex app?"
}
}
function OkayToLeave() {
okayToLeave = true;
}
You'll have the responsibility of setting the variable to true whenever they click a button or link that will take them from that page naturally. I'd use a function for unobtrusive javascript.
Set your event handlers in the DOM ready:
referenceToElement.addEventListener('onClick', OkayToLeave(), false);
This is untested, but should point you in the right direction. Although it may seem like a nuisance to do this, I imagine it's more complete functionality. It covers the cases where a user may click on a favorite, or be redirected from an external application.