JavaScript - Ever Increasing Integer?
Can someone point me in the right direction for using Javascript to increase a number over time without refreshing the page? I also need it to continually go up, and not start at 0 on every refresh.
Is this possible? Blockis Similar TutorialsI already got the following JavaScript code working somewhat (originally taken from a different thread in a different forum and tweaked to give qty ceiling [10] and floor [2] values). However, it seems to work for only one qty variable, and the subtractQty function doesn't seem to subtract properly (at least in Opera 11). Code: <html> <head> <title></title> <style type="text/css"> </style> <script type="text/javascript"> function subtractQty(){ var x=document.getElementById("qty").value; var y=document.getElementById("maxqty").value; if(x - 1 < 2 || y + x <= 40) return; else { document.getElementById("qty").value--; document.getElementById("maxqty").value=y+x*4; document.getElementById("textqty").innerHTML = document.getElementById("qty").value; document.getElementById("textmaxqty").innerHTML = document.getElementById("maxqty").value; } } function additionQty(){ var x=document.getElementById("qty").value; var y=document.getElementById("maxqty").value; if(x >= 10 || y - x < 2) return; else{ document.getElementById("qty").value++; document.getElementById("maxqty").value=y-x*4; document.getElementById("textqty").innerHTML = document.getElementById("qty").value; document.getElementById("textmaxqty").innerHTML = document.getElementById("maxqty").value; } } </script> </head> <body> <form name="f1"> <input type='hidden' name='qty' id='qty' value=2 /> <input type='hidden' name='maxqty' id='maxqty' value=40 /> <div id="textqty">2</div> <input type='button' name='subtract' onclick='javascript: subtractQty();' value='-'/> <input type='button' name='add' onclick='javascript: additionQty();' value='+'/> <div id='textmaxqty'>40</div> </form> </body> </html> What I'd want to happen would be the following: if let's say we have 5 pairs of buttons, making it look something like this: A: 2 [+] [-] B: 2 [+] [-] C: 2 [+] [-] D: 2 [+] [-] E: 2 [+] [-] maxqty: 40 Whenever the user clicks on the [+] button for A, it would subtract (current value + 1) * 4 from maxqty and raise A to 3, then when the user clicks on the [+] for C, it would subtract (current value + 1) * 4 from maxqty and raise C to 3, without having to copy paste multiple functions. I'm already thinking of arrays, but being more used to PHP than Javascript -- and I'm thinking I might actually have to use AJAX for this, although I'm really in the dark with AJAX in particular -- I'm rather confused and somewhat stumped on the coding. Suggestions? Frustrated by the fact that you don't have the integer division operator in JavaScript? Here's a surprisingly little known method: parseInt(19/4) = 4 Now when a VBguru or vbscripter says "ha ha, I can integer divide and you can't!" You now know better. Hi all, this is my first post to this great useful forum. I have a javascript 2d array [x,y] that contains integer values like this: [0,0], [0,1], [0,2], [0,3], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2], [0,-1], [0,-2], [0,-3], [1,-1], [1,-2], [2,-3] my question is how to get the max,min y value for each x. for the sample above it should give me another array with the following results [0,3],[0,-3],[1,2],[1,-2],[2,2],[2,-3] your help is highly appreciated Regards Omran In some cases toPrecision(X) will result in an integer value, which is the expected result. However, it is typical in some fields to always follow the integer value with a decimal and zero (i.e 522 --> 522.0). Technically, this would be incorrect since it would imply an extra digit of precision that does not exist. However, this is often disregarded, provided there are sufficient number of digits (i.e. precision of 5+). That said, is there any way to force an integer result to have a trailing ".0"? Hi all, I am having a problem converting a physical string to a integer. I have a dropdown list of a few items but are numbers though in chracters. I ned to output the value and the text of the selected item. As I cannot use a if-else statement, any help would be great. Code: <select id="std_num" onchange="displayValue()"> <option value="Two">Two</option> <option value="Zero">Zero</option> <option value="Five">Five</option> <option value="Three">Three</option> <option value="Two">Two</option> <option value="Zero">Zero</option> </select> Code: function displayValue() { alert(document.getElementById('std_num').value); var c = (document.getElementById('std_num').value); } Hi, I was hoping you could help me, I was wanting to write text to the screen but without re-writing the whole screen (e.g. document.write();. I wish to take text and put it in a function, modify it, then display it. So far I have: Code: <script type = "text/javascript"> function calculate(text){ var x = "10.0"; var gpa = text.replace(/0.0/g,x); document.getElementById("text").innerText=gpa; } </script> <p id = text >Your Grade Point Average (GPA) is: 0.0</p> <button type="button" onclick="calculate(text)">Calculate</button> Your help would be really appreciated 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.
OVERVIEW of main webpage: when a user logs in, they will be directed to the main page of the website; and will see images displayed according to a value that was stored in the database. the main page preloads an image array: Code: <script type="text/javascript"> var img_FILE = [ 'images/house.png', 'images/car.png', 'images/boat.png' ]; </script> easy enough the database holds a column of integers that are used to access the proper image in the array. i can successfully pull the integer value i need from the db. what i want to do is when the main page loads, i want the value that was pulled from the database to be used to display the correct image. example: lets say i pulled the value 2 from the db. then i want to display img_FILE[2] on the main page when the page "loads". Code: <body> <div id="quadrantOne"> <img id="firstImg" src="images/blankImg.png" width="190" height="235" /> <script type="text/javascript"> $.post('imageScript.php', 'data=0', function(data,status){ if (status == 'success') { imgValue = data } else { imgValue = 'error' } document.getElementById('firstImg').src=img_FILE[imgValue]; }); </script> </div> </body> i debugged imgValue and it is correctly assigned the value coming from the db. but..........the image won't appear on the main page. any ideas how to get this to work? thanks, Paul Williams Another homework assignment that I can't quite seem to get to work... I've been asked to do the following using javascript: -Create a function named randInt() with one parameter of "size". Declare a variable named "rNum" equal to a random integer between 1 and the value of the size variable. Return the value of the "rNum" varialbe from the function. -Create a function named getQuote() with one parameter anemd "qNum". The function should create an array named mtQuotes with five quotes; there should be no quote for the array index "0". Return the value of the mtQuotes array for the qNum index. - In the div element of "quotes" insert a script with the following commands: Declare a variable named "randValue" which is euqal to a random integer between 1 and 5 (use the randInt() function). Declare a variable named "quoteText" containing the quote whose array index value is equal to randValue. Write the value of quoteText to the web page. Here is what I have...it returns undefined. thanks. Code: <html> <head> <script type="text/javascript"> function randInt(size) { var rNum=Math.ceil(Math.random()*5); return(rNum); } </script> <script type="text/javascript"> function getQuote(qNum); var mtQuotes = new Array(); mtQuotes[0] = ""; mtQuotes[1] = "I smoke in moderation, only one cigar at a time."; mtQuotes[2] = "Be careful of reading health books, you might die of a misprint."; mtQuotes[3] = "Man is the only animal that blushes or needs to."; mtQuotes[4] = "Clothes make the man. Naked people have little or no influence on society."; mtQuotes[5] = "One of the most striking differences between a cat and a lie is that a cat has only nine lives."; return mtQuotes[qNum]; </script> </head> <body> <div id="quotes"> <script type="text/javascript"> var randValue=randInt(5); var quoteText=getQuote(randValue); document.write(quoteText); </script> </div> I have some asp:radiobutton lists that need to update a label with the sum of their values each time a user selects a new value. I am brand new to javascripting and would like some insight on how to get this done. I have inserted my code below. Code: <asp:Label runat="server" Text="Greeting:" /> <asp:Label runat="server" ForeColor="Red" ID="lbl_GreetingScore" Text="0" /> <asp:RadioButtonList ID="rdb1_1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Selected="True" Text="N/A" Value="4" /> <asp:ListItem Text="Yes" Value="4" /> <asp:ListItem Text="No" Value="0" /> </asp:RadioButtonList> Well I have an array and a button to select a random integer from the array, how do I make it so that it selects every integer once til all are selected, then it starts over again? For example, an array has these: A B 1 2 Q F So you press the button a number of times, and you get: Q B 1 F A 2 Instead of: 1 Q A 1 B A |