Merging two functions?

C

Charles

Is it possible to merge two functions like these?

<script type="text/javascript">
function showdiv(pass) {
setTimeout('showdiv2("'+pass+'")', 750);
}
function showdiv2(pass) {
var divs = document.getElementsByTagName('div');
for(i=0;i<divs.length;i++){
if(divs.id.match(pass)){
if (document.getElementById)
divs.style.visibility="visible";
}
else {
if (document.getElementById)
divs.style.visibility="hidden";
}
}
}
</script>


Thanks! ;)
 
D

Daz

Charles said:
Is it possible to merge two functions like these?

<script type="text/javascript">
function showdiv(pass) {
setTimeout('showdiv2("'+pass+'")', 750);
}
function showdiv2(pass) {
var divs = document.getElementsByTagName('div');
for(i=0;i<divs.length;i++){
if(divs.id.match(pass)){
if (document.getElementById)
divs.style.visibility="visible";
}
else {
if (document.getElementById)
divs.style.visibility="hidden";
}
}
}
</script>


Thanks! ;)


Yes. Simply append the contents of the first function somewhere near
the bottom of the 2nd (assuming that's where you want it), and change
the first argument of the setTimeout(), so the timer will call it's own
containing function. I am not quite sure of the purpose that your timer
actually servers, but there's also nothing stopping you from adding a
condition statement, and nesting the setTimeout() function inside of
that.

If you can explain what you are trying to achieve, it might be easier
to help you.

It the meantime, here's a similar example, where the function uses a
timer to call upon itself at set intervals, to make text flash. Please
note, this is not perfect as there is no start/stop function for
starters, but it is a simple example which might help you.

Thanks.

Daz.
 
V

VK

Charles said:
Is it possible to merge two functions like these?

<script type="text/javascript">
function showdiv(pass) {
setTimeout('showdiv2("'+pass+'")', 750);
}
function showdiv2(pass) {
var divs = document.getElementsByTagName('div');
for(i=0;i<divs.length;i++){
if(divs.id.match(pass)){
if (document.getElementById)
divs.style.visibility="visible";
}
else {
if (document.getElementById)
divs.style.visibility="hidden";
}
}
}
</script>


function showdiv(pass) {

var $pass = pass;

var divs = document.getElementsByTagName('div');

var pos = 0;

proceed();

function proceed() {
if (divs[pos].id.match($pass)) {
divs[pos].style.visibility="visible";
}
else {
divs[pos].style.visibility="hidden";
}
if (++pos < divs.length) {
window.setTimeout(proceed, 750);
}
else {
divs = null;
}
}
}


Beware of closures.

Typed - not tested: beware of bugs.
 
D

Daz

Daz said:
Charles said:
Is it possible to merge two functions like these?

<script type="text/javascript">
function showdiv(pass) {
setTimeout('showdiv2("'+pass+'")', 750);
}
function showdiv2(pass) {
var divs = document.getElementsByTagName('div');
for(i=0;i<divs.length;i++){
if(divs.id.match(pass)){
if (document.getElementById)
divs.style.visibility="visible";
}
else {
if (document.getElementById)
divs.style.visibility="hidden";
}
}
}
</script>


Thanks! ;)


Yes. Simply append the contents of the first function somewhere near
the bottom of the 2nd (assuming that's where you want it), and change
the first argument of the setTimeout(), so the timer will call it's own
containing function. I am not quite sure of the purpose that your timer
actually servers, but there's also nothing stopping you from adding a
condition statement, and nesting the setTimeout() function inside of
that.

If you can explain what you are trying to achieve, it might be easier
to help you.

It the meantime, here's a similar example, where the function uses a
timer to call upon itself at set intervals, to make text flash. Please
note, this is not perfect as there is no start/stop function for
starters, but it is a simple example which might help you.

Thanks.

Daz.

I can't believe I forgot to add the code...

Here it is:

<html>
<head>
<script type="text/javascript">
function startFlashing() {
var someElement =
document.getElementById('someElement');
someElement.style.visibility =
(someElement.style.visibility == "hidden") ? "visible" : "hidden";
setTimeout('startFlashing()', 500);
}
window.onload = startFlashing;
</script>
</head>
<body>
<span id="someElement">Some Text</span>
</body>
</html>
 
R

Randy Webb

VK said the following on 1/18/2007 5:13 PM:

function showdiv(pass) {

var $pass = pass;

Do you have some perverse irrational reason for redefining a parameter
in a function when there is no need to do so?
 
C

Charles

Thank you guys, actually I mean something like this:
(It doesn't work)


<script type="text/javascript">
function nextpage(page) {
var divs = document.getElementsByTagName('div');
setTimeout('for(i=0;i<divs.length;i++){
if(divs.id.match(page)){
if (document.getElementById)
divs.style.visibility="visible";
}
else {
if (document.getElementById)
divs.style.visibility="hidden";
}
}
}', 750);
}
</script>

What it's going to do is hide all divs and display one single div after
roughly a second.
 
R

RobG

Charles said:
Thank you guys, actually I mean something like this:
(It doesn't work)


<script type="text/javascript">
function nextpage(page) {
var divs = document.getElementsByTagName('div');
setTimeout('for(i=0;i<divs.length;i++){
if(divs.id.match(page)){


This script will be executed in a global context once when the time
expires (about 750 ms). By that time, the references to 'divs' and
'page' will have gone out of scope. Change the script to a function
and you should have more luck.

if (document.getElementById)
divs.style.visibility="visible";


The test for getElementById seems pointless.
}
else {
if (document.getElementById)

As above.
divs.style.visibility="hidden";
}
}
}', 750);
}
</script>

What it's going to do is hide all divs and display one single div after
roughly a second.


What it is more likely to do is hide the div with a specified id after
0.75 seconds. To acheive what you are after, you want to hide all the
divs immediately, then pass the id of the one you want to reveal to
setTimeout.

Try:

function nextpage(id) {
var divs = document.getElementsByTagName('div');
for(i=0; i<divs.length; i++){
divs.style.visibility="hidden";
}
setTimeout( function(){
document.getElementById(id).style.visibility="visible";
}, 750);
}
 
C

Charles

Thanks Rob, yes, this is what I wanted to do. It wasn't working on my
end because I forgot the function(){ stuff (I put the content of the
function directly). Cool deal :)
 
V

VK

Randy said:
VK said the following on 1/18/2007 5:13 PM:



Do you have some perverse irrational reason for redefining a parameter
in a function when there is no need to do so?

Because I mistook the objective by having showdiv() as an universal
timed sequence handler, where it could be called several times with
different pass arguments and each call would start its own timed
sequence. This is what I read out from "Merging two functions?" but
obviously I read way too much out of it. :)

Also to save your emotions: I am not sure that it will work in the way
I just said and I spelled it out in my original post ("not tested").
 
R

Randy Webb

VK said the following on 1/19/2007 3:47 AM:
Because I mistook the objective by having showdiv() as an universal
timed sequence handler,

You seem to do that a lot....
where it could be called several times with
different pass arguments and each call would
start its own timed sequence. This is what I
read out from "Merging two functions?" but
obviously I read way too much out of it. :)

Also to save your emotions: I am not sure that
it will work in the way I just said and I spelled
it out in my original post ("not tested").

At least you are starting to realize the flaws in your ideas. There may
be hope for you yet.
 
R

Richard Cornford

VK said:
Because I mistook the objective by having showdiv() as an
universal timed sequence handler, where it could be called
several times with different pass arguments and each call
would start its own timed sequence.
<snip>

In what way would creating a property of the Variable object with the name
'$pass' and then assigning it the value of the 'pass' property of the same
Variable object make any difference to whether this function "could be
called several times with different pass arguments and each call"?

What Randy's question did was ask you to explain why you did something that
was utterly pointless in your code, and you 'explanation' once again exposes
you as someone who is ignorant of how javascript works, but remains delude
enough to be proposing that what goes on in your mind may represent some
sort of justification of what you write. It is no wonder that you do not
answer 99% of the questions you are asked on this group, as doing so would
more rapidly expose you fantasy misconceptions.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top