I've a function called fadeIn and I want to return it until a condition is met.  I tried to put an alert in the else line along with clearInterval(myVar), but it just keeps alerting.  CSS:  * {     background-color: black;     padding: 0;     margin: 0;     height: 100%; } #title {     background-color: white;     height: 50px; } audio {     display: none; } #audio-icon {     width: 50px;     background-color: white; } #focus {     background-color: black;     margin: auto;     width: 100%;     height: 700px;     border: 5px, solid, grey;     border-style: inset;     border-radius: 10px; } #text-box {     background-color: black;     width: 100%;     height: 200px;     margin-top: 500px;     float: left;     border: 5px, solid, grey;     border-style: inset;     border-radius: 10px; } #command-line {     background-color: black;     width: 100%;     height: 50px;     margin-top: 150px;     float: left;     border: 5px, solid, grey;     border-style: inset;     border-radius: 10px; } #element {     opacity: 0.1; }   HTML:  &lt;audio id="background-audio" controls autoplay="autoplay" loop&gt;     &lt;source src="Darkness.mp3" type="audio/mpeg"&gt; &lt;/audio&gt; &lt;div id="title"&gt;     &lt;img id="audio-icon" src="unmute.png" onclick="mute()"&gt;     &lt;img id="element" src="123.png"&gt; &lt;/div&gt; &lt;div id="focus"&gt;     &lt;div id="text-box"&gt;         &lt;div id="command-line"&gt;&lt;/div&gt;     &lt;/div&gt; &lt;/div&gt;   JavaScript:  function fadeIn() {     var myVar = setInterval(fadeIn, 500);     var element = document.getElementById('element');       if (element.style.opacity &lt; 1) {         element.style.opacity -= '-0.1';     } else {         clearInterval(myVar);      } }  function mute() {     var audio = document.getElementById('background-audio');     var img = document.getElementById('audio-icon');      if (audio.muted) {         audio.muted = false;         img.src = 'unmute.png';     } else {         audio.muted = true;         img.src = 'mute.png';     } }   document.onload = fadeIn();   UPDATE:  I have put the variables outside of the function and all of the sudden my code seems to work correctly. Not sure how this is possible since I have tried this already. My code wouldn't run at all and alert would keep alerting. Anyway thanks for all the help. This was my first question so apart from a few typos and the way I listed my code I don't really know why I get downvoted so any feedback about that is welcome! Here is my current working code:  &lt;script&gt;             var myVar = setInterval(fadeIn, 500);             var element = document.getElementById('element');              function fadeIn() {                 console.log(element.style.opacity)                 if (element.style.opacity &lt; 1) {                 element.style.opacity -= '-0.1';                 } else {                 clearInterval(myVar);                 }             }              function mute() {             var audio = document.getElementById('background-audio');             var img = document.getElementById('audio-icon');              if (audio.muted) {                 audio.muted = false;                 img.src = 'unmute.png';             } else {                 audio.muted = true;                 img.src = 'mute.png';             }         }           document.onload = fadeIn();         &lt;/script&gt;
122
What is the scope of variables in javascript? Do they have the same scope inside as opposed to outside a function? Or does it even matter? Also, where are the variables stored if they are defined globally?
116497
Let me describe the problem in details:  I want to show an absolute positioned div when hovering over an element. That's really simple with jQuery and works just fine. But when the mouse goes over one of the child elements, it triggers the mouseout event of the containing div. How do I keep javascript from triggering the mouseout event of the containing element when hovering a child element.   What's the best and shortest way to do that with jQuery?  Here is a simplified example to illustrate what I mean:  Html:  &lt;a&gt;Hover Me&lt;/a&gt; &lt;div&gt;   &lt;input&gt;Test&lt;/input&gt;   &lt;select&gt;     &lt;option&gt;Option 1&lt;/option&gt;     &lt;option&gt;Option 2&lt;/option&gt;   &lt;/select&gt; &lt;/div&gt;   Javascript/jQuery:  $('a').hover( function() { $(this).next().show() }               function() { $(this).next().hide() } );
23978
Is it possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle()?  How would you test if an element is visible or hidden?
78101
Is it possible to see the return value of a method after the line has been run and before the instruction pointer returns to the calling function?  I am debugging code I can't modify (read: don't want to re-compile a third party library), and sometimes it jumps to code I don't have source to or the return expression has side effects that stop me being able to just run the expression in the Display tab.  Often the return value is used in a compound statement, and so the Variables view will never show me the value (hence wanting to see the result before control returns to the calling function).  UPDATE: I can't use the expression viewer as there are side-effects in the statement.
98146
Every time I toggle active class in a list (to dynamically change DOM content) I find that I always go to this approach to check and clear which class is active. I'm still fairly new to using pure javascript so I'm wondering if there is a better, simpler approach?  It feels like having to run a second function to clear out all active classes seems grunty.   I've included an example of how I do it.    var specialNav = document.getElementById("special-nav");  if (specialNav) {   var navItems = specialNav.getElementsByTagName('li');   var i;   for (i = 0; i &lt; navItems.length; i++) {     navItems[i].addEventListener("click", function() {       if (!this.classList.contains('active')) {         clearActives(navItems);         this.classList.toggle('active');       }     });   } }  function clearActives(classlist) {   if (classlist) {     for (i = 0; i &lt; classlist.length; i++) {       classlist[i].classList.remove('active');     }   } } .active {   color: red }  li {   cursor: pointer; } &lt;div id="special-nav"&gt;   &lt;ul&gt;     &lt;li class="active"&gt; 1&lt;/li&gt;     &lt;li&gt;2&lt;/li&gt;     &lt;li&gt;3&lt;/li&gt;     &lt;li&gt;4&lt;/li&gt;   &lt;/ul&gt; &lt;/div&gt;
7437
I have a method that uses the  function and makes a call to another method. On initial load method 2 works fine. However, after the timeout, I get an error that says method2 is undefined. What am I doing wrong here?  ex:  test.prototype.method = function() {     //method2 returns image based on the id passed     this.method2('useSomeElement').src = "http://www.some.url";     timeDelay = window.setTimeout(this.method, 5000); };  test.prototype.method2 = function(name) {     for (var i = 0; i &lt; document.images.length; i++) {         if (document.images[i].id.indexOf(name) &gt; 1) {             return document.images[i];         }     } };
23477
I tried the following code to get an alert upon closing a browser window:  window.onbeforeunload = confirmExit; function confirmExit() {   return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?"; }   It works, but if the page contains one hyperlink, clicking on that hyperlink raises the same alert. I need to show the alert only when I close the browser window and not upon clicking hyperlinks.
73636
I want to make a toggle button for my custom operator. It works similar to checkbox: once the button is clicked, the operator keeps running until the button gets another click, so will the button itself greyed out. I tried to achieve the grey-out effect by implementing poll() function like this:  @classmethod def poll(cls, context):           return context.area.type == 'VIEW_3D' and context.region.type == 'WINDOW' and plane_toggle_flag   Every time the button is clicked I want the toggle state to change, so in the operator's init(), I write:  def __init__(self):        plane_toggle_flag=not plane_toggle_flag   However, it seems as long as poll() returns false, the init() function never get the chance to be called. Any idea how to solve this? Thank you in advance.
73955
I am having a little trouble figuring out how to make this work properly, I am trying to put a small countdown clock in the left column and one in the right column, which will show a countdown to two separate dates. I was able to create a single countdown clock using the below code and was hoping to be able to be able to tweak the code enough to allow it to run on a second CEWP with a different date. Is it possible to run two Javascript files on the page without them interferring with eachother? Thank you anyone that can lend a quick hand.      &lt;style type="text/css"&gt; .container{     text-align: center;     background: #f63536;   font-family: sans-serif;   font-weight: 100;   height:115px;   width:215px; }  h3{   color: #FFFFFF;   font-weight: 100;   font-size: 16px;   margin: 0px 0px 5px;   padding-top:3px; }  #clockdiv{     font-family: sans-serif;     color: #fff;     display: inline-block;     font-weight: 100;     text-align: center;     font-size: 20px; }  #clockdiv &gt; div{     padding: 5px;     border-radius: 3px;     background: #ff5b5b;     display: inline-block; }  #clockdiv div &gt; span{     padding: 5px;     border-radius: 3px;     background: #ab0202;     display: inline-block; }  .smalltext{     padding-top: 5px;     font-size: 13px; }  &lt;/style&gt;  &lt;div class="container"&gt;  &lt;h3&gt;Countdown Clock Left&lt;/h3&gt; &lt;div id="clockdiv"&gt;   &lt;div&gt;     &lt;span class="days"&gt;&lt;/span&gt;     &lt;div class="smalltext"&gt;Days&lt;/div&gt;   &lt;/div&gt;   &lt;div&gt;     &lt;span class="hours"&gt;&lt;/span&gt;     &lt;div class="smalltext"&gt;Hrs.&lt;/div&gt;   &lt;/div&gt;   &lt;div&gt;     &lt;span class="minutes"&gt;&lt;/span&gt;     &lt;div class="smalltext"&gt;Min.&lt;/div&gt;   &lt;/div&gt;   &lt;div&gt;     &lt;span class="seconds"&gt;&lt;/span&gt;     &lt;div class="smalltext"&gt;Sec.&lt;/div&gt;   &lt;/div&gt; &lt;/div&gt;&lt;/div&gt;  &lt;script&gt; function getTimeRemaining(endtime){   var t = Date.parse(endtime) - Date.parse(new Date());   var seconds = Math.floor( (t/1000) % 60 );   var minutes = Math.floor( (t/1000/60) % 60 );   var hours = Math.floor( (t/(1000*60*60)) % 24 );   var days = Math.floor( t/(1000*60*60*24) );   return {     'total': t,     'days': days,     'hours': hours,     'minutes': minutes,     'seconds': seconds   }; }  function initializeClock(id, endtime){   var clock = document.getElementById(id);   var daysSpan = clock.querySelector('.days');   var hoursSpan = clock.querySelector('.hours');   var minutesSpan = clock.querySelector('.minutes');   var secondsSpan = clock.querySelector('.seconds');    function updateClock(){     var t = getTimeRemaining(endtime);      daysSpan.innerHTML = t.days;     hoursSpan.innerHTML = ('0' + t.hours).slice(-2);     minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);     secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);      if(t.total&lt;=0){       clearInterval(timeinterval);     }   }    updateClock();   var timeinterval = setInterval(updateClock,1000); }  var deadline = 'October 31 2015 00:00:50'; initializeClock('clockdiv', deadline); &lt;/script&gt;
50286
I have a funciton that I am currently using to show a hidden div.a_type  How can I modify this code so that instead of fading in the hidden div, I can add the new div to the DOM   jQuery(function(){   // Add Answer      jQuery(".add_answer").click(function(){       if(count >= "4"){         alert('Only 4 Answers Allowed');         }else{       var count = $(this).attr("alt");       count++;       $(this).parents('div:first').find('.a_type_'+count+'').fadeIn();       $(this).attr("alt", count);     }      });   });     Ok, now that i have this sorted out, i have one more question,  I have another function that removes the inserted div's if a button is clicked. It's not working now that the additional divs are not loaded into the dom on pageload. How can i trigger the function to remove these now?  jQuery(function(){   // Hide Answer  jQuery(".destroy_answer").click(function(){   $(this).parents("div:first").fadeOut(function (){ $(this).remove() });    var count = $(this).parents('div:first').parents('div:first').find('.add_answer').attr("alt");    count--;    $(this).parents('div:first').parents('div:first').find('.add_answer').attr("alt", count);  });    });
121420
I have put together the following mootools script   window.addEvent('domready', function() {     var shouts = "timed.php";     var log = $('log_res');     function updateData (url,target)     {         new Ajax(url,{         method: 'get',          update: $(target),          onComplete: function() {         log.removeClass('ajax-loading');}  }).request();         log.empty().addClass('ajax-loading');     }      var update = function(){ updateData ( shouts, 'log_res' ); };     update();           // call it immediately     update.periodical(10000);   // and then periodically   });   heres the html  &lt;div id="AJAX"&gt;     &lt;h3&gt;Ajax Response&lt;/h3&gt;     &lt;div id="log_res"&gt;exercise&lt;/div&gt; &lt;/div&gt;   its using moo 1.1.  The above works fine, the page loads then the ajax request kicks in div id log_res has a class of ajax-loading whilst its updating, and when its finished the text exercise in the div is replaced with whatever the ajax has returned (yippee). But I want to put some custom HTML into the div WHILST the page is loading, because the ajax-loading class is not enough to contain the information, i also want to put a spinny flasher into the div whilst the ajax request is retrieving the information. Hope that makes sense!
35404
I want to find the first span element after class counter, in code something like this:  &lt;div class="counter"&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;span&gt;&lt;/span&gt;   It seems like the next() function only finds the immediate next element, so something like this:  $(".counter").next("span")   Won't work. The way I have been using is a bit lengthy and I was wondering if there was a shorter way, it is this:  $(".counter").nextAll("span").eq(0)   I think the closest() method in jQuery 3 will do the trick, but I am using 1.2.6 -- is there a better way to do this (am I just using next() wrong?)
58025
I simply want one command block to test for the time of day or night (e.g. time = 18000) and output a message in the chat to everyone if that time returns true.  I was trying a simple set-up with two command blocks and a comparator in the middle, but I'm a beginner and am obviously missing the correct syntax (especially to test for a particular time of day, and if true, then trigger the say "message" block).  ETA: Kudos to @Ben for the Daylight Sensor/Redstone idea, but I'd really prefer a Command Block solution for the above, since my plan is to output several messages during the day and night cycle at very specific times in the world. Thanks.
111663
In a set of if/elif/else/fi statements, I have made 'else' present the user with an error message, but I also want it to take the user back to the question which was asked before the if/else statements so that they can try to answer it again.  How do I take the user back to a previous line of code? Or, if this is not possible, is there another way to do this?
4059
The situation is somewhat like-  var someVar = some_other_function(); someObj.addEventListener("click", function(){     some_function(someVar); }, false);   The problem is that the value of someVar is not visible inside the listener function of the addEventListener, where it is probably being treated as a new variable.
9151
How can I check the existence of an element in jQuery?  The current code that I have is this:  if ($(selector).length &gt; 0) {     // Do something }   Is there a more elegant way to approach this? Perhaps a plugin or a function?
108478
I got this identifier inputevenmousebutton is not declared in the current scope; should I use another identifier?  func _ready():     position = get_viewport_rect().size / 2     direction.x = rand_range(-1, 1)     direction.y = rand_range(-1, 1)     direction = direction.normalized()     width = get_viewport_rect().size.x     height = get_viewport_rect().size.y  func _process(delta):     position += direction * speed * delta     if position.x &lt; 0 or position.x &gt; width:         direction.x = -direction.x     if position.y &lt; 0 or position.y &gt; height:         direction.y = -direction.y  func _on_UFO_input_event( viewport, event, shape_idx ):     if event is InputEvenMouseButton and event.button_index ==          BUTTON_LEFT and event.pressed:         direction.x = rand_range(-1, 1)         direction.y = rand_range(-1, 1)           direction = direction.normalized()         position.x = rand_range(1, width -1)         position.y = rand_range(1, height -1)         speed += 5