0

HTML:

<div class="container-fluid">
  <div class = "row text-center" align = "middle">
    <h2>Pseudo Random Quote Machine</h2>
  </div>
  <div class = "row text-center" align = "middle">
    <div class = "col-xs-12 well message" id = "quoting"> 
    </div>
  </div>
  <div class = "row text-center" align = "middle">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Random Quote
      </button> 
      <button id = "tweet" class = btn btn-primary>Tweet this!
      </button>
    </div>
  </div>
</div>

Javascript:

function randomQuotes() 
{ 
  //array of quotes
  var quotes = [ "\"Operator! Give me the number for 911!\" - Homer J. Simpson", "\"I\'m normally not a praying man, but if you\'re up there, please save me Superman\" - Homer J. Simpson", "\"I\'m in no condition to drive...wait! I shouldn\'t listen to myself, I\'m drunk!\" - Homer J. Simpson", "\"A Degenerate, Am I? Well You Are A Festizo. See, I Can Make Up Words Too, Sister\" - Peter Griffen", "\"Victory Shall Be Mine!\" - Stewie Griffen", "\"He's a spy, blow him up. I'm gonna go take a shit.\" - Rick Sanchez", "\"Ohh yea, you gotta get schwifty. \" - Rick Sanchez", "\"Yo! What up my glip glops!\" - Rick Sanchez", "\"WUBBA LUBBA DUB DUBS!!! \" - Rick Sanchez", "\"Existence is pain to a meeseeks Jerry, and we will do anything to alleviate that pain.\" - Mr. Meeseeks", " \"It\'s morphine time\" - TheRussianBadger"]; 

  var randNum = Math.floor((Math.random() * quotes.length));
  document.getElementById("quoting").innerHTML = quotes[randNum];
}

function tweetThis()
{
  var tweetToShare = document.getElementById("quoting").innerHTML;
  var tweetUrl = 'https://twitter.com/share?text=' +   encodeURIComponent(tweetToShare) + ".";
  window.open(tweetUrl);
}

document.getElementById("getMessage").onclick = function(){randomQuotes();}
document.getElementById("tweet").onclick = function(){tweetThis();}

Basically, any time I click either of the two buttons the function keeps being called over and over again. Here is the code running in Code Pen

I guess my questions would be:

  1. why would the functions be called more than one time ?

  2. What should I do to either stop the functions from being called multiple times or prevent it from happening in the first place?

5
  • Working fine on Chrome 58. Commented May 28, 2017 at 21:53
  • Yep, also fine on firefox 53. Commented May 28, 2017 at 21:56
  • @cancer, you are absolutely right. Thank you. Commented May 28, 2017 at 22:05
  • @yuriy636 thanks for checking. Commented May 28, 2017 at 22:06
  • @Andreas thank you for also checking. Commented May 28, 2017 at 22:07

1 Answer 1

1

functions seem to be called only one time on chrome. Maybe your browser cached an old script: maiusc+f5 to clean

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

1 Comment

A common way this happens is if you have developer tools open on chrome and your still working on your Js file. Having developer tools open on chrome enables caching, you can fix this by going developertools -> network -> disable cache

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.