SetTimeout Seems To Return A Simple Integer As It's "id" Which Can Be Used To Clear The Timeout Ahead Of Time - Is Right / Legal
Oct 25, 2011
I've noticed that setTimeout seems to return a simple integer as it's "id" which can be used to clear the timeout ahead of time. So, in order to have multiple setTimeouts called (which requires the previous one to be cleared before the next one is called), I simply did this:
clearTimeout(setTimeout(function() {
/* some code */
}, 1000) -1);
Note the "-1"... each time this code is called, it starts a new setTimeout and clears "instance-1" which is supposed to be the previous instance. The idea is that this block can be called hundreds of times, and when the calling finally stops, the inner code is executed 1 second later. This SEEMS to be working (yes, even in MSIE!). Question is, am I fooling myself? Is this wrong?
Does JavaScript have an easy of converting an ordinary integer like 135 into a time format like 02:15? I'm working on a timer that handles the backend with just a plain integer that counts down every second but it needs to output looking like a clock.
Got 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?
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it wrong because the function that calculates the new coordinates for the picture returns 32 million and change instead of the coordinate. This causes the picture to not be able to appear on the display, obviously.
Here's the calling code (snipped for readability) that works without setTimeout:
What I want is an element with a shifting background image:
<html><head> <script type="text/javascript"> var x = 0 var y = 0 inc = 4 function bgWander(el){ x = x + Math.round(2*inc*Math.random())-inc y = y + Math.round(2*inc*Math.random())-inc el.style.backgroundPosition = x + 'px ' + y + 'px' setTimeout('bgWander(el)',200) } </script></head><body>
I'm trying to graph line with a delay between each line drawn - my code is: var jg = new jsGraphics('Canvas'); jg.setColor('maroon'); jg.drawLine(40,130,80,120); setTimeout('jg.paint()',10000); jg.drawLine(80,120,120,110); setTimeout('jg.paint()',10000); Unfortunately it draws all the lines at the same time i.e the setTimeout function doesn't like the paint method.
I'm new to JavaScript, and have been playing around with a few simple functions to get going. However, I've hit a problem that I just can't fix, I'm trying to write a simple function to animate the collapse of a div using setTimeout (I know, jQuery does it a lot better), and it's gotten the best of me. The only error it's giving me now is
Code: missing ; before statement on line 25 but I can't see why. I assume it's something to do with my abuse of the setTimeout syntax (why does it insist that everthing is enclosed in quotes.I'm sure there are much better scripts than mine, and searching the forum I've come across Vic Phillips' fine specimen which mostly went over my head. But if I just wanted it to work I'd use jQuery - I'm more interested in why it's not working.
code is below. In addition to this I've also got an external script to compensate for browsers without getElementsByClass, but it should work in modern browsers up to the point where it doesn't!
I am trying to learn how to do this one thing with JavaScript and can't seem to find anything on the internet. I have learned about loops and arrays and some other useful stuff. I've been looking for 2 days straight. I have gotten close though.
I am trying to have a textbox on the page and a button below it. A statement above the textbox should ask the user to enter a positive integer and then click the button. When the button is clicked the results from 0 to that number should appear below the button. Should look like this
please enter positive integer. 6 BUTTON1
1 2 3 4 5 6
I have been trying night and day for 2 days now and It's driving me insane.
I want a script to receive a time in the 24 hour time format HH:mm (e.g. 18:30) and remove 15 minutes from it.The problem is that if the time inputted was 00:00, then how do I get to 23:45?
I'm sure it's easy if you're in the know.I can do this with PHP, but not with JS.I just need the function / basic line of code that does this. I can work out how to call it etc. myself.
I wrote a little live clock in JS (using Steve Levithan's "date format" code) that formats the time display according to a format string. I then find all elements by classname and search for a classname of "clock" and write the clock string to the innerHTML of every element I find.But recently I added another "feature". Rather than hard code a format string, I instead pass it in ID. Example:
<span class="clock" id="hh:mm:mm tt"></span>
..and the JS:
var doClock = function () { var now = new Date(); var e = getElementsByClassName('clock');
[code]...
Note that I am passing the clock FORMAT STRING by using the span's ID. It works great...
I am trying to populate the javascript and want to return the value to the jsp page.. The function is like this.
function difference(startTime, endTime){ var starttime = document.getElementById(startTime).value; var endtime = document.getElementById(endTime).value;
Can a regex be used that will satisify the one int/one letter rule that will work in IE5 or if not, can the above function be refactored w/o having to interate through the string array each time? Neither method works in Opera 6. Also, is there any condition except for validating perhaps a password where such a rule would be needed?
I'm using ajax to grab a response from a PHP page, on success, the ajax sets a variable. After the line that calls the ajax function is the line the returns the variable (a global variable). Problem is, because the variable is returned right after the ajax function is fired, it doesn't give the ajax enough time to set the variable. The work around I've thought about would be using the ajax function as a variable, and returning that variable in the initial return line.
Here's some code for reference:
The function that will return the response I'm trying to collect:
What I need when I select a date in in_date field I have to get 1 year from now in out_date. For example if in in_date I select "11/19/2010" I have to get out_date as 11/20/2011 I tried like
I would like to use a radio button to control the datepicker so that it changes the days that can be selected.
i.e. radio choice 1 - user can only select four days in advance. radio choice 2 - user can only selecttwo days in advance. radiochoice 3 - user can only selectone day in advance.
I am using the Keith Wood version of the datepicker : [url]
This is the code I am currently using for four days:
I have a search form and added the "Clear All" Button functionality...but it clears the last part of the search form display preferences....how do I only clear the checkboxes at the top of the form only and not at the bottom of the form between the <DIV> tags? I have attached a copy of the JSP page.
I have the following function that's supposed to say "Please make a guess" 20 seconds after an initial confirmation is displayed. However, it's immediately displayed as soon as someone hits "Cancel". If I change it's time to 40000 (20 seconds after the initial function is called), it does do it 40 seconds total, so it kind of does what I want. So it seems that the second setTimeout is initiated from the time the script is called generally, but I'm looking for a way specifically to have the 20 seconds start only after the "cancel" button is hit.
I use an HttpRequest in my web application to launch an heavy computation server side. The result is then send back to the browser. The problem I got is that when the computation took too much time, then I never get the result. I have to reload the page to get it.
I assume it's because my socket connection has time out, but my event listener does not seems to get any event. Maybe I miss something:
function xmlhttpChange() { // looking fo a xmlhttp "loaded" for (var i in nodeId2xmlHttp) { if ((nodeId2xmlHttp[i] != null) && nodeId2xmlHttp[i].readyState == 4) { if (nodeId2xmlHttp[i].status==200) { nodeId2xmlHttp[i] = null; } else { alert("Problem retrieving XML data (nodeId: " + i + ")"); nodeId2xmlHttp[i] = null; } } } }
My question is : how to handle XMLHttpRequest timeout. Any pointers?
I'm trying to decrease my numeric variable in random increments from 0-4 every 5 seconds until this variable reaches 0. Basically it's a countdown, but has a random increment each count. Here's my code:
with jquery, i try to get the margin-left ($('#mydiv').css('margin-left'), but the function return 0px, unable to retrieve the good value (auto) anyone has idea to retrieve the value "auto" when margin-left is "auto" ?