|
|
Author |
Thread |
fred_X
Nervous Wreck (II) Inmate
From: Insane since: Nov 2003
|
posted 11-18-2003 12:18
Hello
I am trying to delay this code from working after the page loads for 4 seconds but my setTimeout does not appear to be working. Can someone help please.
Thank You
<head>
<script>
function delay(){
setTimeout("initProp()",4000) // DELAY
}
</script>
</head>
<body onload="delay()">
<script>
var width=document.body.clientWidth;
var height=document.body.clientHeight;
function slider(who,step,timeOut,type) {
if(type==0)
var where="top";
if(type==1)
var where="bottom";
if(type==2)
var where="left";
if(type==3)
var where="right";
eval('var temp=document.getElementById(who).style.'+where);
temp=parseInt(temp);
if(type==0| |type==1)
var checkWith=height/2;
if(type==2| |type==3)
var checkWith=width/2;
if(-temp<checkWith) {
temp-=step;
eval('document.getElementById(who).style.'+where+'=temp;');
setTimeout("slider('"+who+"',"+step+",'"+timeOut+"',"+type+")", timeOut);
}
else {
document.getElementById(who).style.display="none";
document.body.scroll="no";
}
}
function initProp(type,div1bg,div2bg,div1bw,div2bw,div1bc,div2bc,step,timeOut,click) {
if(type==0) {
var bWhere1="border-bottom";
var bWhere2="border-top";
var putZero1="top:0px; left:0px";
var putZero2="bottom:0px; left:0px";
document.write('<div id="sliderDiv1" style="z-index:100; display:block; position:absolute; '+putZero1+' ; background-color:red; width:'+(width)+'; height:'+(height/2)+'; '+bWhere1+':'+div1bc+' solid '+div1bw+'px"></div>');
document.write('<div id="sliderDiv2" style="z-index:100; display:block; position:absolute; '+putZero2+' ; background-color:red; width:'+(width)+'; height:'+(height/2)+'; '+bWhere2+':'+div2bc+' solid '+div2bw+'px"></div>');
if(!click) {
slider('sliderDiv1',step,timeOut,0);
slider('sliderDiv2',step,timeOut,1);
}
}
}
</script>
<script>
new initProp(0,'','',5,5,'#000000','#000000',2,0);
</script>
</body>
|
poi
Paranoid (IV) Inmate
From: France Insane since: Jun 2002
|
posted 11-18-2003 17:39
fred_x: Welcome in the asylum.
Your setTimeout works, but leaved a call to the initProp() function in the last script. Remove it and put the right call ( with the arguments ) in the setTimeout: code:
function delay()
{
setTimeout( "initProp(0,'','',5,5,'#000000','#000000',2,0)", 400 ) // DELAY
}
BTW, there's no need to instanciate the initProp().
Whatever, on my computer ( W2K IE6 or FB0.7 ) your script doesn't work either, 'coz the DIVs are not "ready" when the script try to execute the eval('var temp=document.getElementById(who).style.'+where);. Since you use the DOM to retrieve the handle of the DIVs, you could also use it to create and insert them in the document tree. That way, everything should work.
Your script, and especially the slider function, is complex so I took the liberty to rewrite it : code:
function delay()
{
setTimeout( "newSlide( 2,1, 0 )", 4000 )
}
function newSlide( delay, step, percentage )
{
divId = "sliderDiv"
sliderDivHandle = document.getElementById( divId )
if( !sliderDivHandle )
{
newDiv = document.createElement( "DIV" )
newDiv.id = divId
with( newDiv.style )
{
position = "absolute"
left = 0
top = 0
width = document.body.clientWidth
height = document.body.clientHeight
border = "solid #c00"
}
document.body.appendChild( newDiv )
sliderDivHandle = document.body.lastChild
}
percentage = Math.min( 100, percentage+step )
documentHeight = parseInt( document.body.clientHeight )
borderWidth = documentHeight*(100-percentage)/200
sliderDivHandle.style.borderWidth = borderWidth +"px 0 "+ borderWidth +"px 0 "
if( navigator.appVersion.indexOf( "MSIE" )==-1 ) // adjustement of the height in Standard Compliant browsers
sliderDivHandle.style.height = (documentHeight-2*borderWidth) +"px"
if( percentage!=100 )
setTimeout( "newSlide( "+ delay +", "+ step +", "+ percentage +" )", delay )
else
sliderDivHandle.style.display = "none"
}
It works in IE6.0, FB0.7 and MZ1.4.
Cheers,
Mathieu "POÏ" HENRI
|
fred_X
Nervous Wreck (II) Inmate
From: Insane since: Nov 2003
|
posted 11-18-2003 22:15
Hello Poi and thanks for the welcome & assist
I looked at what you said and to be honest I got myself in a lost situ when I followed your advice. I kept getting an error saying object expected.
But the original code does still function. Alas STLL without the delay on loadup. you can see it in action here http://mysite.freeserve.com/tlrs2/index.html
I've added this to the code
function delay()
{
setTimeout( "initProp( 2,1, 0 )", 4000 ) // DELAY
}
But when I replace it with the below as you advised in its place. I get the object error message
function delay()
{
initProp(0,'','',5,5,'#000000','#000000',2,0)",4000) //DELAY
}
I dont know a great deal about script writing so any ideas you have will be greatly received.
BTW
Your re-vamping of the original code was VERY appreciated. However this has been somewhat of a learning course for me and I would like to see it through to the end if you see my point.
Hope this hasnt offended you in anyway.
Thanks again
|
fred_X
Nervous Wreck (II) Inmate
From: Insane since: Nov 2003
|
posted 11-22-2003 17:13
|
fred_X
Nervous Wreck (II) Inmate
From: Insane since: Nov 2003
|
posted 11-25-2003 15:32
Am I rubbing people up the wrong way in this forum by either posting a subject that is of no interest to anyone? Or By counter replying to assistance that was given which didnt quite work out as expected and needs a little fine tuning so's I can get it right?
This is a help forum isnt it? and I am asking for help am I not? Why do I feel like I have to almost beg.
To quote a recent comment on a post in this forum "When will you try your best before moaning for a kind soul to write the code for you ? Do yourself a favor : gain in autonomy."
Something I happen to agree with. But when you best efforts fail its time to seek help. As I'm doing
If no one wants to help me refine the small problem I'm having with the original post.......Would they be kind enough to tell me to piss off. least I will know where I stand.
Thanks
|
Slime
Lunatic (VI) Mad Scientist
From: Massachusetts, USA Insane since: Mar 2000
|
posted 11-25-2003 17:09
Well, looking at the original post, the reason the setTimeout isn't doing you any good is because the last thing in the code you showed us is
new initProp(0,'','',5,5,'#000000','#000000',2,0);
You're calling initProp right there (with an unnecessary "new"), which is why it gets called right away. Maybe you should remove that line.
|
poi
Paranoid (IV) Inmate
From: France Insane since: Jun 2002
|
posted 11-25-2003 18:48
quote: To quote a recent comment on a post in this forum "When will you try your best before moaning for a kind soul to write the code for you ? Do yourself a favor : gain in autonomy."
You should have pushed your investigation a little further and had a look at the impressive number of requests made by Hiroki during the last 20 days to figure that he was heavily relying on the asylum while he should, IMHO, have tried to be a little more atonomous.
Whatever, since then Hiroki and I have had a kind discussion by mail and agreed that we both made some mistakes.
Now, let's get back to your javascript problem.
What Slime said correspond to the first part of my post. As you and I said a problem remains because your DIVs are not "ready" when the slider( ) function tries to move them. Thus my suggestion to use the DOM to create the DIVs. If you don't know how to do that, you can pick some bits in my code.
The second part of my post ( a.k.a. the big chunk of code ) was supposed to COMPLETELY replace your code. It only differs from your own one in that that it doesnt have the 5px black borders, but trust me it works. I wouldn't post some code I haven't tested. Since you have had difficulties to understand what it does, I've uploaded a page with a documented version of it to see it in action. Hope that'll help.
I'm not sure why my first post mislead you. Was I not clear enough, did the presentation fooled you at the point that you misunderstood how to use the big chunk of code. I assumed that you had understood the code you have posted so I thought you'd have no problem to read/understand mine. My code seemed clear to me, but since it's MY code it's normal if it seems obvious to me
Then your post felt in oblivion and I begged on some other inmates to help you in a way I hadn't considered so far. There's several Javascript gurus here, but they don't spend all their free wandering in the asylum. I don't know if and when some of them saw that thread.
Hope that wouldn't turn you out of the asylum.
Cheers,
Mathieu "POÏ" HENRI
[This message has been edited by poi (edited 11-27-2003).]
|
fred_X
Nervous Wreck (II) Inmate
From: Insane since: Nov 2003
|
posted 11-26-2003 08:49
Ok!
Disregard any of the above posts and lets start afresh. I've made some adjustments.
If you would'nt mind taking a look at this link all that I have done and require is contained within, and you can see the actual code in action. http://mysite.freeserve.com/tlrs2/index.html
2 questions are all I have.
What do I put in it to get what I want? and
Where does it go?
NEVER has a damned timeout caused me so much trouble. No wonder people go bald:P
Thanks fellas
|
poi
Paranoid (IV) Inmate
From: France Insane since: Jun 2002
|
posted 11-26-2003 14:27
fred_x: Since you want the "doors" to be there right at the beginning and to open 4 seconds after, you can remove the onload event in the body tag and the delay( ) function in your page. Then replace the if( !click ){ ... } statement by this one : code:
if( !click )
{
window.onload = function ()
{
setTimeout( "slider('sliderDiv1',"+ step +","+ timeOut +",0);slider('sliderDiv2',"+ step +","+ timeOut +",1);", 4000 );
}
}
I explain : Your call to initSlider(0,'','',5,5,'#000000','#000000',2,0); will create the DIVs right at the beginning and hide the content of the page. The window.onload = function() { ... } will delay the opening of the "doors" of 4 seconds after the loading of the page.
Does it fit more your requirements ?
Mathieu "POÏ" HENRI
|
MajorFracas
Nervous Wreck (II) Inmate
From: Insane since: Jul 2003
|
posted 11-26-2003 18:07
Try modifying the following two lines which invoke "slider()" as follows below:
code:
if(!click) {
setTimeout("slider('sliderDiv1',"+step+","+timeOut+",0)",4000);
setTimeout("slider('sliderDiv2',"+step+","+timeOut+",1)",4000);
}
The problem would appear that you were not getting the delay between when the DIVs where added to the document and when the animation begins. Since the one function "initSlider" does both, the animation begins immediately.
|
poi
Paranoid (IV) Inmate
From: France Insane since: Jun 2002
|
posted 11-26-2003 18:47
MajorFracas: your post comes a little late. And you forgot to take into account the time taken to retrieve all the content of page ( it includes the html itself, but also the images, scripts ... ). Ok I'm picky here, but 4seconds may not be enough to load everything on a 56K.
No offense, really, me and my twisted mind found that funny to see a similar post comming 4hours later. Anyway if it helps fred_X to understand better, well, that's fine
Mathieu "POÏ" HENRI
|
fred_X
Nervous Wreck (II) Inmate
From: Insane since: Nov 2003
|
posted 11-26-2003 21:57
Ok!
First off. Thanks MajorFracas for your input towards my problem, it worked just fine and it seems to be a slight variation on Poi?s post so its good to know that there?s several ways of achieving the same result.
Now then, Poi
It not easy bestowing gratitude in text, Facial expressions go along way towards really defining ones feelings. But suffice to say I?m all smiles here. Not JUST for the code you provided that resolved this issue but also for the dedication and effort you also showed in guiding me towards the eventual goal. And the way in which you broke down your own code to explain what and why it does, what it does.
Your assistance in this matter was exemplary, I can only say thank you for your help, but it does mean a lot more than that, No doubt due to the considerable frustration and number of hours I?ve spent trying to get the problem resolved.
At the very least I?ve gained knowledge and am fully aware I have a lot to learn.
Regarding the earlier post in which you asked if I had problems with your ?big? code? Well I think that was covered on the web page I posted.
As a final note to the forum in general:
So as to avoid a recurrence of my previous snotty post, are there certain criteria one should be aware of before posting a plea for help? Am I expecting too much? I ask only because this is my first time in a forum, any forum and usually I try to achieve most things on my own merits before asking others.
Thanks again Poi
Fred_X
|
MajorFracas
Nervous Wreck (II) Inmate
From: Insane since: Jul 2003
|
posted 11-27-2003 13:52
Poi: Sorry about that. I swear I didn't see your last post yesterday when I was developing mine... strange.
|
poi
Paranoid (IV) Inmate
From: France Insane since: Jun 2002
|
posted 11-27-2003 17:00
MajorFracas: No problem. Weird. The asylum is attacked by a bunch gremlins screwing up a thread some times, but generally it results in a tag soup that is resolved by simply posting a new message.
fred_X: Glad, you finally got it working.
As for the forum in general, I don't think there's some clearly stated rules, but to gain some time you should include as much informations as you can ( tested browsers , wanted browsers, wanted behavior, current behavior, things to avoid ... ) notice the things you've tested before so nobody suggest them again. Whenever possible give a link to see the code in action. Oh, and if you post some code, there's one thing not everybody does though quite helpful, try to indent it. It's painful to read a flat code.
Cheers,
Mathieu "POÏ" HENRI
|
smonkey
Paranoid (IV) Inmate
From: Northumberland, England Insane since: Apr 2003
|
posted 11-27-2003 19:39
Hey Poi,
Something that has been bothering me for sometime is that whenever I copy and paste code into a new post the handling script strips out all my indent spaces.
I know that is what the UBB Code CODE is for, but I have been recommended not to use it since it makes copying and pasting from the post a problem (everything tends to get posted in notepad or whatever as a big long string) so what is the best thing to do?
Just a niggle.
visit my CryoKinesis Online Gallery
|
poi
Paranoid (IV) Inmate
From: France Insane since: Jun 2002
|
posted 11-27-2003 20:32
smonkey: To keep your indentation, you must enclose your script in the [ code] ... [/code] UBB tags. Unfortunately the linebreaks are purely and simply replaced by a <BR> which causes the long string problem when pasted into a text editor. If the administrators of the Asylum could replace the \n by <BR>\n, the problem would be solved.
Sorry as the Asylum is, I don't know which way of posting code is the best. It mainly depends of what you want the readers to do with it. Read it easily, or use it directly. Obviously you can upload your script somewhere but it's not as convenient as seeing it in the post.
Nonetheless if the Asylum stays as is, we could write our code in the appropriate UBB tags and add a ¶ character to figure the linebreaks. It's ugly as hell but that way the "pasters" could replace them by some real linebreaks provided that tey use a descent editor that support to enter the linebreak character in the replace options panel.
The following piece of code is just a test to see if the ¶ solution looks like crap or not ( though I already have an idea about that ): code:
¶ function delay()
¶ {
¶ setTimeout( "newSlide( 2,1, 0 )", 400 )
¶ }
¶ function newSlide( delay, step, percentage )
¶ {
¶ alert( "pasglop" )
¶ divId = "sliderDiv"
¶ // try to retrieve the DIV
¶ sliderDivHandle = document.getElementById( divId )
¶ if( !sliderDivHandle )
¶ {
¶ // ....
¶ }
¶ // ....
¶ }
[EDIT]
Well, since it's not quite convincing let's try something else : posting the code without the UBB tags and by replacing the spaces and tabs by some Alt+0160 characters ( because they corresponds to entity ). Alas that way, we loose the monotoype font which highly increase the legibility of the [ code] tags.
function delay()
{
setTimeout( "newSlide( 2,1, 0 )", 400 )
}
function newSlide( delay, step, percentage )
{
divId = "sliderDiv"
alert( "glop" )
// try to retrieve the DIV
sliderDivHandle = document.getElementById( divId )
if( !sliderDivHandle )
{
// ....
}
// ....
}
That solution seems to work, even in NotePad.
[/EDIT]
Mathieu "POÏ" HENRI
[This message has been edited by poi (edited 11-27-2003).]
|