JavaScript - Meaning Of Settimeout()'s Return Value
I'm troubleshooting some issues with menus closing and when I alert the return value from something like:
Code: timer = setTimeout("closeMenu(id)",500); I get 2,3,4. It seems to increment. Does that just represent how many times the timeout code has been executed? Similar TutorialsGot a timing issue with sending a URI string out to a server side script right before a client side form validation process returns true.. The problem is that the return true gets called to quickly for the submission of the URI string to be completed. So my question is, might there be a way to mix a return and a setTimeout? If so, could some one give a an example? Thanks! This prbl. appears only in IE: Code: l2 = obj.parentNode.getElementsByTagName("select")[0].value; --> object does not support this property or method Code: var l2 = obj.parentNode.getElementsByTagName("select")[0].value; --> OK explanation needed. Quote: Traversal also aids developers in affecting the elements immediately surrounding an element that is being manipulated or otherwise utilized by a script. This can range from adding a class to parent elements to indicate activity to disabling all inactive form elements to any number of other useful tasks. can't understand the above,the paragraph is from a book.what the author want to tell.anyone helps.thank you, the HTML code: Code: <ul id="ul1"> <li>1111</li> <li>2222</li> </ul> <div id="div1"></div> the javascript code: Code: var oDiv=document.getElementById('div1'); oDiv.style.left=getPos(this).left+this.offsetWidth+'px'; oDiv.style.top=getPos(this).top+'px'; function getPos(obj){ var aPos={left: null, top: null}; while(obj) { aPos.left+=obj.offsetLeft; aPos.top+=obj.offsetTop; obj=obj.offsetParent; } return aPos; } why it write the while loop,and what's effection of the while loop obj=obj.offsetParent; what's this line meaning? hello .. I want know what is the mean of these symbol : || {} thanks .. function args() { return arguments; } don't know the "return arguments; "meaning. I have 3 Qs about ajax(1,2) and syntax meaning(3): 1- I want to appear a list of urls from a db table to browser, with their newest Google PAGE RANK every time appear in the webpage, and this rank saved auto to database along with previous.... google provides any web service for this? tell me URLs? I found difficulty contact google... 2- Do you know any internet web service (programming) that you input a Russian word, and get (true or 1) if valid, or (false or 0) if not valid word? 3- what mean the in file attached statements to html main file? script.js?x=5 or style.css?p=5 or xmlfile.xml?x=777. I don't understand the logic of Break, Return False, Return True. It was never really covered in our college class, and I see everyone using it. I got an A in the class, if that 'proves' that I really tried to apply myself. NOTE: I understand what the function is doing. I just don't understand WHEN to use break, return false or return true if the the translator can determine the conditional statements. PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } else return false; } if the the translator can determine the conditional statements, why not write it like this: PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; // what's the point for the 'break'? Won't the rest of the code be ignored since it passed the first condition? } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } // why not leave the last else out? is it just a 'safety' catch, in case something other than true or false is inputted? else return false; // what's the point? } Questions: Why use return true, if the translator knows it's ture? Why use "return false" if the translator knows it's false and the alert window has already gone up? why not use "break" to stop the code? Why use the "return false" at the end "else" statement? Hi room, Hey, I opened up the source code for this page in google chrome and since i'm learning javascript, i wanted see if i could "read" it and figure out what was going on. I'm am having the hardest time understanding "return false" and "return true". Could someone step me through this via interpreting this code (in bold typeface): Code: var DefaultValue = 'Search'; function clearSearch() { if (document.searchForm.q.value == DefaultValue) { document.searchForm.q.value = ''; } } function validateSearchHeader() { if ( document.searchForm.q.value == '' || document.searchForm.q.value.toLocaleLowerCase() == DefaultValue.toLocaleLowerCase() ) { alert('Please enter at least one keyword in the Search box.'); document.searchForm.q.focus(); return false; } return true; } Thanks! Okay, for some reason the setTimeout isn't working for me, and I have no idea why. I've tried everything and Googled even more. If anyone has any ideas, I'll be grateful Code: function display(min,sec) { if (sec <= 0) { sec=60; min-=1; } if (min <= -1) { sec=0; min=0; tEnd(); } else { sec = sec-1; } if (sec<=9) { sec = "0" +sec; } document.form.time.value=min+":"+sec; SM = window.setTimeout("display("+min+","+sec+")",1000); } function tEnd() { window.clearTimeout(SM); alert('Your time is up!'); } Thanks in advance. Hi trying to load a random swf files using setTimeout please help Code: </head><script type="text/javascript"> var numberOfSongs = 3 var sound = new Array(numberOfSongs+1) sound[1]= "number/1.mp3" sound[2]= "number/2.mp3" sound[3]= "number/3.mp3" function randomNumber(){ var randomLooper = -1 while (randomLooper < 0 || randomLooper > numberOfSongs || isNaN(randomLooper)){ randomLooper = parseInt(Math.random()*(numberOfSongs+1)) } return randomLooper } var randomsub = randomNumber() var soundFile = sound[randomsub] document.write ('<EMBED src= "' + soundFile + '" hidden=true autostart=true loop=false>') setTimeout(randomNumber,4000); </script> <body onload="randomNumber()"> Is there another JS function that I can use to force a time delay without having the code continue to run? I'm using the setTimeout() function but the call to this function doesn't stop the code flow. I need to stop the code flow while waiting. I guess I need a Sleep() type of JS function. What I'm trying to do is display some blank text on the screen using a for loop (I'm using <BR> to give the appearance of "opening up" a vertical window section in the browser) and I need this 'text' to complete before allowing the following code in the function to execute. The following code will display a Table with rows of data. I'm trying to use a timing function to give a visual impression of a window opening up just before the data displays. Thanks... Hello - I've got this lovely little animation that slides some div fields to the right a short distance. Everything works except the setTimeout (located in the SlideIn function). It simply 'jumps' without taking any time at all. I've tried a bunch of stuff: changed the amount of time from 100 to 10000 changed it from var t = setTimeout("SlideIn()", 1000) SlideIn with/without the brackets, with/without the quotation marks. SlideIn() SlideIn(i) SlideIn(label_id_fixed) SlideIn(label_id_no) Just about at the end of my rope, and so many different combinations I'm forgetting which ones I've tried and which I haven't. I know it is looping through because it gets to the end position and allows the other JavaScript functions to work ok. So it is not getting 'stuck' or anything like that. Here is the complete code (there is a style sheet located separately). Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="class.css" /> </head> <script type="text/javascript"> //<![CDATA[ window.onload=starter; function starter(){ collapseMenu(); SetUpAnimation(); } function SetUpAnimation(){ if (document.querySelectorAll) { labels = document.querySelectorAll('div.label'); } else if (document.getElementsByClassName) { labels = document.getElementsByClassName('label'); } //loops through the labels if (labels){ for (var i=1; i <= labels.length; i++) {SlideIn(i); } } } function SlideIn(label_id_no){ var label_id_fixed = label_id_no; var endPos = 150; var currentPos = 0; for (currentPos = 1; currentPos < 60; currentPos++){ var label_id_name = "label" + label_id_fixed; document.getElementById(label_id_name).style.left = currentPos + "px"; setTimeout("SlideIn", 1000); } } function collapseMenu(){ var elems = null; var labels = null; if (document.querySelectorAll) { elems = document.querySelectorAll('div.elements'); labels = document.querySelectorAll('div.label'); } else if (document.getElementsByClassName) { elems = document.getElementsByClassName('elements'); labels = document.getElementsByClassName('label'); } if (elems){ for (var i=0; i < elems.length; i++) { elems[i].style.display="none"; } for (var i=0; i < labels.length; i++) { labels[i].onclick=showBlock; } } } function showBlock(evnt){ var theEvent = evnt ? evnt : window.event; var theSrc = theEvent.target ? theEvent.target: theEvent.srcElement; var itemId = "elements" + theSrc.id.substr(5,1); var item = document.getElementById(itemId); if (item.style.display=='none'){ item.style.display='block'; }else{ item.style.display='none'; } } //]]> </script> </head> <body> <div class="label" id="label1">this is a label</div> <div class="elements" id="elements1"> <p>painting</p><br /> <p>photography</p><br /> </div> <div class="label" id="label2">this is another label</div> <div class="elements" id="elements2"> <p>sculpture</p><br /> <p>ceramics</p><br /> </div> </body> </html> Hello guys I need to get something fun! with setTimeout function! I am n00b! so be patent please. I need when <body onload="Myfunc();"> fires, that function should show "Please wait...!" or "Loading...". for , say 5 seconds!. then it disappear. I used setTimeout with that but it didn't do what I wanted! here is my code: PHP Code: function Myfunc(){ document.getElementById("ss2").innerHTML = "Loading..."; setTimeout("Myfunc();", 5000); } // where id="ss2" is the place to display the string "loading..." ! and a one more question ! can I do something like a while loop! where it holds *i* value as seconds! and for every second it passes it should print out a string I make it up ! let say loop for 3 seconds ! So, when seconds = 0 then display "string of second 0" and stick it in there, then go to the next second when it is exactly = 1 then display "new line with string of second 1" and stick it in there, then do to the last second when it is exactly = 2 then display " new line with string of second 2" and stick it in there, exit the loop! is there anyway to do that? please note that I am a n00bie ! help is much appreciated ! hi, i'm making a page with an image on it. when that image is clicked i want it to make one div visible, and also run the javascript function to make a second div visible after 2 seconds. Everything seems to work correctly except that the javascript runs on page load instead of waiting for when the image is clicked. any help much appreciated. thanks! Here's the javascript: <code <script type="text/javascript"> function showIt() { document.getElementById("show_1").style.visibility = "visible"; } setTimeout("showIt()", 2000); </script> </head> </code> Here's the html: <code> <div id="flyer1">content <div id="show_1" style="visibility:hidden">content </div> </div> <div id="image"> <a href="javascript:void(0)" onclick="MM_showHideLayers('flyer1', '','show')"; "showIt()">image</a> </div> </code> I'm working on a disjointed rollover, with several links changing an image in another location. There is a default image that appears when the page opens and whenever you roll off any of the links. Initially, the problem was that mousing from one link to the next caused a quick flash of the default image because there is a little space between these links. To address that, I added a time out to keep the default image from appearing immediately on onmouseout. Now, when I mouse from one link to the next I get a smooth transition, but then the delayed onmouseout function kicks in while still hovering over a link. How can I make hovering over a new link override this delayed onmouseout? Here's the script: Code: <script type="text/javascript"> window.onload = preloader() function preloader(){ var pic1 = new Image(); pic1.src = "images/cover1.jpg"; var pic2 = new Image(); pic1.src = "images/cover2.jpg"; var pic3 = new Image(); pic1.src = "images/cover3.jpg"; var pic4 = new Image(); pic1.src = "images/cover4.jpg"; } function bookInfoSwap(imgSrc,ID,bInfo) { document.getElementById("covers").src = imgSrc; document.getElementById("courseID").innerHTML = ID; document.getElementById("bookInfo").innerHTML = bInfo; } function swapDelay() { setTimeout("bookInfoRevert()",3000); } function bookInfoRevert() { document.getElementById("covers").src = "images/covers.gif"; document.getElementById("courseID").innerHTML = " "; document.getElementById("bookInfo").innerHTML = " "; } </script> ...and a couple of the links: Code: <a href="http://www.mymathlab.com/" target="_blank" onmouseover="bookInfoSwap('images/cover6.jpg','Self Study Course ID: <br />cos68804','Billstein, <em>A Problem Solving approach to Mathematics for Elementary School Teachers, 9/e and Custom Edition</em>');" onmouseout="swapDelay();" class="courseTab">Math 10/11</a> <a href="http://www.mymathlab.com/" target="_blank" onmouseover="bookInfoSwap('images/cover4.jpg','Self Study Course ID: <br />cos59796','Sullivan, <em>Statistics: Informed Decisions Using Data, 3/e</em>');" onmouseout="swapDelay();" class="courseTab">Math 21</a> Thanks. Im fairly new to javascript, i have mostly been into php i have this code i wrote in one of my projects and i assume theres an easier way Code: setTimeout("check20()", 0); setTimeout("check19()", 50); setTimeout("check18()", 100); setTimeout("check17()", 150); setTimeout("check16()", 200); setTimeout("check15()", 250); setTimeout("check14()", 300); setTimeout("check13()", 350); setTimeout("check12()", 400); setTimeout("check11()", 450); setTimeout("check10()", 500); setTimeout("check9()", 550); setTimeout("check8()", 600); setTimeout("check7()", 650); setTimeout("check6()", 700); setTimeout("check5()", 750); setTimeout("check4()", 800); setTimeout("check3()", 850); setTimeout("check2()", 900); setTimeout("check()", 950); basically every 50 javascript seconds a function is activated, so that means i have a **** ton of function too all doing the same thing just to different ids If you know of a better way let me know please Hi there The JavaScript below behaves erratically - sometimes the timing is perfect, at other times it show 20 mins has passed when only 6 has in reallity - quite some gain - I'm baffled! Code: Public Function SessionTime(ByVal DivName As Panel) As String Dim vTime As String = "var sessionTimeout = 19; " & vbCr vTime += "function ReadCookie(cookieName) " & vbCr vTime += "{ " & vbCr vTime += "var theCookie=""""+document.cookie; var ind=theCookie.indexOf(cookieName); " & vbCr vTime += "if (ind==-1 || cookieName=="""") return """";" & vbCr vTime += "var ind1=theCookie.indexOf(';',ind); " & vbCr vTime += "if (ind1==-1) ind1=theCookie.length; " & vbCr vTime += "return unescape(theCookie.substring(ind+cookieName.length+1,ind1));" & vbCr vTime += "} " & vbCr vTime += "function DisplaySessionTimeout()" & vbCr vTime += "{ " & vbCr 'assigning minutes left to session timeout to Label vTime += "if (sessionTimeout > 1) " & vbCr vTime += "{ " & vbCr vTime += "document.getElementById('" & DivName.ClientID & "').innerText = sessionTimeout + "" Minutes remaining until session timeout!""; " & vbCr vTime += "} " & vbCr vTime += "else " & vbCr vTime += "{ " & vbCr vTime += "document.getElementById('" & DivName.ClientID & "').innerText = ""ONLY ONE MINUTE REMAINS UNTIL SESSION TIMEOUT!""; " & vbCr vTime += "}" & vbCr vTime += "sessionTimeout = sessionTimeout - 1; " & vbCr vTime += "var Posted = ReadCookie('PostedValue'); " & vbCr vTime += "if (Posted == 0)" & vbCr vTime += "{ " & vbCr vTime += "sessionTimeout = 19; " & vbCr vTime += "var expires = new Date(); " & vbCr vTime += "expires.setUTCFullYear(expires.getUTCFullYear() + 1); " & vbCr vTime += "document.cookie = 'PostedValue=1; expires=' + expires.toUTCString() + '; path=/'; " & vbCr vTime += "}" & vbCr 'if session is under preset warning vTime += "if (sessionTimeout > 5) " & vbCr '<--------------- 4 vTime += "{ " & vbCr vTime += "document.getElementById('" & DivName.ClientID & "').style.visibility=""hidden""; " & vbCr vTime += "}" & vbCr vTime += "else " & vbCr vTime += "{" & vbCr vTime += "document.getElementById('" & DivName.ClientID & "').style.visibility=""visible""; " & vbCr vTime += "}" & vbCr 'if session is not less than 0 vTime += "if (sessionTimeout >= 1) " & vbCr 'call the function again after 1 minute delay vTime += "{ " & vbCr vTime += "setTimeout('DisplaySessionTimeout()', 60000); " & vbCr '<------60000 vTime += "} " & vbCr vTime += "else " & vbCr vTime += "{ " & vbCr 'show message box vTime += "alert(""Your current Session will expire in approx one minute""); " & vbCr vTime += "} " & vbCr 'vTime += "alert(""Set to "" + ReadCookie('PostedValue')); " & vbCr vTime += "} " & vbCr vTime += "DisplaySessionTimeout();" & vbCr Return vTime End Function I want to delay image load for 3 seconds and here is the code but it doesnt work... HELP PLEASE <img id="img444" src="http://imakeinternet.com/squeeze/finish.png" style="display: none;"> <script> function showimage() { document.getElementById("img444").src="http://imakeinternet.com/squeeze/finish.png"; } setTimeout ("showimage();", 3000); </script> [CODE] function slide(dir, i) { //getW() gets the width of the window //i is initally set to 0 var w = getW(); if(i < w) { document.getElementById('page').style.left = (document.getElementById('page').offsetLeft - 1)+"px"; i++; setTimeout("slide("+dir+", "+i+")", 100); } } [/ CODE] the function executes from it's initial calling but never calls itself back up |