2

Why doesn't this script run when I submit a number? (The purpose is that the temp counter slowly decrements to the users input)

        var s,t,d; 
s = document.getElementById('start');
t = document.getElementById('temp');
d = document.getElementById('display');

startChange = function(){
  var v,nt,diff,timelapse,decrease,decreaseit,loop;
  v = +d.value;
  nt = +t.value; 
  diff = v-nt;
  timelapse = 500; //set to whatever you like
  decrease = .2;
  decreaseit = function(){
    var v = d.value;      
    if(v>=(nt+decrease)){
     loop = setTimeout(function(){    
      d.value = (v-decrease).toFixed(1);
      decreaseit();
     },timelapse)   
    } else clearInterval(loop);
  }
  decreaseit();

}

s.onclick = startChange;

HTML

    <input id="display" type="text" value="7.2" disabled>

    <input id="temp" type="text">
    <button type="button" id="start">Set temp</button> 

1 Answers1

3

Because your s variable is looking for an element with an ID of "start", and your button's ID is "startChange".

krillgar
  • 11,554
  • 6
  • 43
  • 81
  • also the exit condition of this loop seems incorrect. – Schien Feb 18 '14 at 02:01
  • `s.onclick = startChange();` I think is something else. Perhaps @Schien can help with the exit condition of the loop, cause I'm not quite seeing what he is. At the very least, this will now call into the function, and you can debug in the Javascript console. – krillgar Feb 18 '14 at 02:14
  • 2
    @user3091110: You have to convert the values to numbers first, otherwise `nt+decrease` will preform string concatenation: http://jsfiddle.net/8fnt9/. It would have been easier to help you if you explained what *exactly* didn't work. For example, your original problem was that the element with such an ID wasn't found, so the code through an error and did not even run. After fixing that the code runs, the value is decreased, but it doesn't stop at the correct value. The **better** you explain the problem, the **easier** it is for us to help you. – Felix Kling Feb 18 '14 at 02:17
  • changing from "start" to "startChange" should at least make the script run. please confirm that you are seeing this before further debugging. – Schien Feb 18 '14 at 02:18
  • I made the changes from @FelixKling but the script stil doesnt run. – user3091110 Feb 18 '14 at 02:24
  • Then please edit your question with exactly what it is supposed to do, what it's currently doing, and where it is breaking. My answer was just the first quick thing I saw. Without further information from you, I wasn't going to rewrite your entire script for you. – krillgar Feb 18 '14 at 02:26
  • 1
    @user3091110: But I hope you do see that the jsFiddle runs, right? If it *still* doesn't run for you, then you are most likely running the code before the DOM elements exist. Please see http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element for a solution to that problem. – Felix Kling Feb 18 '14 at 02:32
  • 1
    @user3091110: You shouldn't change the code in your question with the suggested changes, because it makes this answer now completely useless (and even wrong). – Felix Kling Feb 18 '14 at 02:33
  • @FelixKling why shouldn't I? Doesnt it now give a better view over the actual situation, so you can help me out with the current code why it still isnt working? – user3091110 Feb 18 '14 at 02:35
  • 1
    You should leave a comment in it where you changed the ID, as @Felix Kling said to preserve the integrity of people trying to help you for future visitors. Also, please give more explanation with comments in your code, or more text aside from your code. Right now, it's like you're just tossing a problem and wanting us to figure it all out for you. That's not what this site is for. – krillgar Feb 18 '14 at 02:36
  • 1
    @user3091110: For the reason I mentioned in my comment: *"because it makes this answer now completely useless (and even wrong)."*. Integrity is the word that I was missing. And I already referred you do another question which should solve the problem. Note that my jsFiddle demo works, at least it does what you described it should do. If you are not able to integrate it in your existing code, then there is not much we can do. I can only advice you to [learn how to debug JavaScript](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) and figure it out yourself. – Felix Kling Feb 18 '14 at 02:38
  • I see that the jsFiddle works too here. But it isnt working in the browser. – user3091110 Feb 18 '14 at 02:46
  • 1
    @user3091110: *"it isnt working in the browser."* jsFiddle is a website and so it runs in the browser as well. Your statement doesn't make a lot of sense. – Felix Kling Feb 18 '14 at 02:56
  • I mean it works via jsFiddle, but not via the file that I created via netBeans. (on the browser) – user3091110 Feb 18 '14 at 12:35