Jump to content

Issues with restarting onclick functions when parsing variables.


bluebomber
 Share

Recommended Posts

Up until now, the method I've used to prevent onclick triggered functions from falling over each other (should the user rapidly click the mouse button) is to create a "are you ready to be triggered?" if loop with a variable which needs to be at "1" before the function can begin, as soon as the function starts the variable is set to 0 and is only returned to 1 right at the end to stop the user from starting the function while it's already in progress.This has worked well, but I've come across a new problem I'm unsure of how to fix.I have a sequence of functions (starting with newpage():) that are triggered when different buttons are pushed on a website, the buttons themselves parse a different string into a variable called "page" when they are clicked, the sequence of functions will go on to display, hide and change the opacity of DIV elements by reading this parsed string and using that as reference to know which element ID to access.The problem I have, is that even though my "pageready" variable prevents multiple functions being triggered on top of each other, the "page" variable still gets parsed through from the HTML on multiple clicks, so should the user click on a variety of different buttons on the website in quick succession the sequence of functions start to reference elements by the wrong ID's half way through the sequence itself.Does anyone know of a simple way to prevent this from happening?Here is my codevar pageopac = 0;var pagechromeopac = 0;var pageieopac = 0;var firstvisit = 1;var pageready = 1;function newpage(){if (pageready == 1){ pageready = 0; if (firstvisit == 1){ firstvisit = 0; loadpage(); } else { fadepage(); } }}function fadepage (){if (pageopac >0){pageopac--;pagechromeopac = pageopac/10;pageieopac = pageopac*10;document.getElementById(currentpage).style.opacity = pagechromeopac;document.getElementById(currentpage).style.filter="alpha(opacity="+pageieopac+")";setTimeout("fadepage()",20); }else{document.getElementById(currentpage).style.display = 'none';loadpage();}}function loadpage(){document.getElementById(page).style.display = 'block';if (pageopac <= 10){ pageopac++; pagechromeopac = pageopac/10; pageieopac = pageopac*10; document.getElementById(page).style.opacity = pagechromeopac; document.getElementById(page).style.filter="alpha(opacity="+pageieopac+")"; setTimeout("loadpage()",20); }else{ currentpage = page; pageready = 1; }}

Link to comment
Share on other sites

I don't see where you're using the page variable, but if you're setting it inline in the onclick code, you should change it to be a parameter to the newpage function, and only update the global variable if the function is going to do its thing.

Link to comment
Share on other sites

Apologies yes - it's used in the html - for example onclick="newpage(page='people');"I'm quite new to this sort of thing, I'm not quite sure what you mean in the rest of your message.My understanding is that passes over the value "people" as the page variable just for use within the newpage function.

Link to comment
Share on other sites

No, it sets the value of the global page variable, then passes the same value to the function. You should just be doing this:newpage('people');Now you're just passing the value to the function. That function can do its checks, and then only set page if the checks pass.

function newpage(p){  if (...)  {	page = p;	...  }}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...