1

hey i was just wondering if someone could help me work out way my script is not working i'm trying to get it to update the speed for the setInterval using a input number box but it does not seen to update the value each time the value is change in the input and i really don't get why this is happening any ideas why?

var nametags = null;
var nametag = document.getElementById('names');
var speed = document.querySelector('input[id="speeds"]');

var partyTags = function() {
  clearInterval(nametags);
  if (nametags !== null) {
    nametags = null;
  } else {
    var delay = speed.value;
    nametags = setInterval(function() {
      document.getElementsByClassName('hud-party-tag')[0].value = '&#' +
        Math.random().toString(9).substring(9, 5) + ' ' + [nametag.value] + ' ' + '&#' +
        Math.random().toString(9).substring(9, 5);
    }, delay);
  }
}

speed.addEventListener("input", partyTags);

partyTags();
<input type="number" id="speeds" value="1000">
<input type="text" maxlength="35" id="names" value="NAME HERE">
<br>
<br>
<input type="text" class="hud-party-tag" disabled>

3
  • Seems to be working.. what is the expected output? jsfiddle.net/naho6gcL Commented Oct 4, 2018 at 4:25
  • convert speed variable into integer: var speed = parseInt(document.querySelector('input[id="speeds"]')); Commented Oct 4, 2018 at 4:51
  • change var nametags = null; to var nametags; Commented Oct 4, 2018 at 5:04

2 Answers 2

1

The problem was on the check of nametag for NULL value, in particular when the input event was detected, the first time you call partyTags() it enters into the IF condition and set nametag to NULL, after this the method finished and the interval isn't updated. The next call to partyTags() enters the ELSE section and update the interval, and so on... So, try next code and see if it works now:

var nametags = null;
var nametag = document.getElementById('names');
var speed = document.querySelector('input[id="speeds"]');

var partyTags = function()
{
    clearInterval(nametags);
    var delay = speed.value;

    nametags = setInterval(
        function()
        {
            document.getElementsByClassName('hud-party-tag')[0].value = '&#' +
            Math.random().toString(9).substring(9, 5) + ' ' + [nametag.value] + ' ' + '&#' +
            Math.random().toString(9).substring(9, 5);
        },
        delay
    );
}

speed.addEventListener("input", partyTags);

partyTags();
<input type="number" id="speeds" value="1000">
<input type="text" maxlength="35" id="names" value="NAME HERE">
<br>
<br>
<input type="text" class="hud-party-tag" disabled>

Sign up to request clarification or add additional context in comments.

Comments

0

Remove this call in the code

partyTags();

you called partyTags method twice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.