                             callback(err, result)
 
 
-    run: (query, delay, callback) =>
-        @index++
-        @timers[@index] = {}
+    # Run the query every `delay` ms
+    # Returns a timeout number (to use with @stop_timer).
+    # If `index` is provided, `run` will its value as a timer
+    run: (query, delay, callback, index) =>
+        if not index?
+            @index++
+        index = @index
+        @timers[index] = {}
         ( (index) =>
             @connect (error, connection) =>
                 if error?

            TermBase.run = ->
                throw new Error("You should remove .run() from your queries when using the Data Explorer.\nThe query will be built and sent by the Data Explorer itself.")

    connect: (callback) ->
        r.connect @server, callback

    run_once: (query, callback) =>
        @connect (error, connection) =>
            if error?

                                query.private_run connection, (err, result) =>
                                    if typeof result?.toArray is 'function'
                                        result.toArray (err, result) =>
                                            # This happens if people load the page with the back button
                                            # In which case, we just restart the query
                                            if err?.message is "This HTTP connection is not open"
                                                return @run query, delay, callback, index
                                            callback(err, result)
                                            if @timers[index]?
                                                @timers[index].timeout = setTimeout fn, delay
                                    else
                                        if err?.message is "This HTTP connection is not open"
                                            return @run query, delay, callback, index
                                        callback(err, result)
                                        if @timers[index]?
                                            @timers[index].timeout = setTimeout fn, delay
                            )()
        )(index)
        index

    # Stop the timer and close the connection
    stop_timer: (timer) =>
        clearTimeout @timers[timer].timeout
        @timers[timer].connection.close {noreplyWait: false}
