I have a main function with a setTimeout function inside of it that uses a random interval each time the main function is called. Once the timeout triggers it will run the main function again and create a new timeout. However I can manually run the main function multiple times, continuously generating additional randomly timed setTimeouts. I don't want this to happen, I need the timeouts to overwrite each other each time they are made so that only one can exist at a time. I have applied a couple of fixes already which seem to work in theory but in practice fail. Here is my code: mainFunc {     var timeout;     console.log(timeout)     clearTimeout(timeout);     timeout = setTimeout(mainFunc(), randomTime) }  As you can see clearing the timeouts and restating the variable both fail to change the behavior of the program. Any ideas are greatly appreciated.
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?
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];         }     } };
52759
Maybe I'm doing something wrong, but shouldn't the commands (run separately) notify-send -t 1 &quot;test&quot; notify-send -t 1000 &quot;test&quot; notify-send -t 10000 &quot;test&quot;  have different timeouts? The first being nearly instantaneous, the second taking 1 sec and the third 10 seconds? In all cases it seems to take about six seconds for me. Is there a way around this behaviour? As the developers label this as a &quot;feature&quot; instead of a bug, I would like some alternatives.
48266
Is there some way to do multi-threading in JavaScript?
77364
I have a program that continually polls the database for change in value of some field. It runs in the background and currently uses a while(true) and a sleep() method to set the interval. I am wondering if this is a good practice? And, what could be a more efficient way to implement this? The program is meant to run at all times.  Consequently, the only way to stop the program is by issuing a kill on the process ID. The program could be in the middle of a JDBC call. How could I go about terminating it more gracefully? I understand that the best option would be to devise some kind of exit strategy by using a flag that will be periodically checked by the thread. But, I am unable to think of a way/condition of changing the value of this flag. Any ideas?
2127
I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event?   I want the user to be able to stop the repeated refresh of data.
84148
I want to know how to make something happen only once like a tutorial in a game which appears only when you first start your game and then when your game got saved to a further point it never appears again even when you close your game and start again.  I want to give players some amount of gold when they first got their hands on my game ... but I do not want to give them that again and again whenever they restart the game ...  So I want to know how to make something happen only once in a game application..  I hope you get my point ... thanks ..
20807
I have heard about a buffer overflow and I would like to know how to cause one.  Can someone show me a small buffer overflow example? New(And what they are used for?)
32423
I want to change the default timeout in Chrome. In Firefox I can set the value of network.http.connection-timeout in about:config - but how can I do that in Chrome?
42864
I mean other than using it when required for functions, classes, if, while, switch, try-catch.  I didn't know that it could be done like .  In the above link, Eli mentioned that "They use it to fold up their code in logical sections that don't fall into a function, class, loop, etc. that would usually be folded up."  What other uses are there besides those mentioned?   Is it a good idea to use curly braces to limit the scope of your variables and expand the scope only if required (working on a "need-to-access" basis)? Or is it actually silly?   How about using scopes just so that you can use the same variable names in different scopes but in the same bigger scope? Or is it a better practise to reuse the same variable (if you want to use the same variable name) and save on deallocating and allocating (I think some compilers can optimise on this?)? Or is it better to use different variable names altogether?
87845
I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called  that purports to do exactly that:  import timeit  def foo():     # ... contains code I want to time ...  def dotime():     t = timeit.Timer("foo()")     time = t.timeit(1)     print "took %fs\n" % (time,)  dotime()   However, this produces an error:  Traceback (most recent call last):   File "&lt;stdin&gt;", line 1, in &lt;module&gt;   File "&lt;stdin&gt;", line 3, in dotime   File "/usr/local/lib/python2.6/timeit.py", line 193, in timeit     timing = self.inner(it, self.timer)   File "&lt;timeit-src&gt;", line 6, in inner NameError: global name 'foo' is not defined   I'm still new to Python and I don't fully understand all the scoping issues it has, but I don't know why this snippet doesn't work. Any thoughts?
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.
62595
i am writing script which uses different Tools of arcgis and implementing different functionalities .. My script consists in different Functions running in an order. Each function is connected to each other , and works on layers and performs   functions like(update cursor, search cursor ) When function1 finishes execution funciton2 starts and works on the same layer on which first one was working.   The problem is here function2 generates wrong results or works wrong.  works wrong means if a loop is defined to 5 iterations it will runs more then 5 times. like 20, 30 times and generates different results of one value.  When i run each function separately it will works fine. but when i run function in one script it will create problems.  My findings of this are LOCKS which are creating the problem.  Sample Structure of my script:  import arcpy   import math   import sys     import traceback     from arcpy import env    class test:       def __init__(self, workSpace):          self.workSpace = workSpace        # Add Fields       def function1(self):             # Script               return;           # Generate Report       def function2(self):              # Script            rows = arcpy.SearchCursor(self.mapLayerFinal,"","","Shape_Area")           row = rows.next()           while row:                   area = row.Shape_Area                     print "function2"                         destination_cursor = arcpy.UpdateCursor(self.censusLayer, '"DISTRICT_ID" = '+ str(district_id))                   row1 = destination_cursor.next()                   while row1:                           row1.setValue("District_Area",area)                           destination_cursor.updateRow(row1)                            row1 = destination_cursor.next()                    row = rows.next()             return;        t = test(workSpace)   t.function1()   t2 = test(workSpace)   t2.function2()     NOTE: code is tested and working correct if each function is executed separately
46209
Using instance methods as callbacks for event handlers changes the scope of this from "My instance" to "Whatever just called the callback". So my code looks like this  function MyObject() {   this.doSomething = function() {     ...   }    var self = this   $('#foobar').bind('click', function(){     self.doSomethng()     // this.doSomething() would not work here   }) }   It works, but is that the best way to do it? It looks strange to me.
24071
I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n?  def f(n, x):     n = 2     x.append(4)     print('In f():', n, x)  def main():     n = 1     x = [0,1,2,3]     print('Before:', n, x)     f(n, x)     print('After: ', n, x)  main()   Output:  Before: 1 [0, 1, 2, 3] In f(): 2 [0, 1, 2, 3, 4] After:  1 [0, 1, 2, 3, 4]
88514
I want a method to run after every 10 or 15 seconds.   My timer is set to:   timer = (double) gameTime.ElapsedGameTime.TotalSeconds;   What would be the logic to check for every 10 or 15 seconds?
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;