Viewing all the timeouts/intervals in javascript?

I don’t think there is a way to enumerate active timers, but you could override window.setTimeout and window.clearTimeout and replace them with your own implementations which do some tracking and then call the originals. window.originalSetTimeout = window.setTimeout; window.originalClearTimeout = window.clearTimeout; window.activeTimers = 0; window.setTimeout = function(func, delay) { window.activeTimers++; return window.originalSetTimeout(func, delay); }; window.clearTimeout = … Read more

Calculating Page Load Time In JavaScript

Why so complicated? When you can do: var loadTime = window.performance.timing.domContentLoadedEventEnd- window.performance.timing.navigationStart; If you need more times check out the window.performance object: console.log(window.performance); Will show you the timing object: connectEnd Time when server connection is finished. connectStart Time just before server connection begins. domComplete Time just before document readiness completes. domContentLoadedEventEnd Time after DOMContentLoaded event … Read more

Will setInterval drift?

Short answer: No, you can’t be sure. Yes, it can drift. Long answer: John Resig on the Accuracy of JavaScript Time and How JavaScript Timers Work. From the second article: In order to understand how the timers work internally there’s one important concept that needs to be explored: timer delay is not guaranteed. Since all … Read more

How can I pause setInterval() functions?

You could use a flag to keep track of the status: var output = $(‘h1’); var isPaused = false; var time = 0; var t = window.setInterval(function() { if(!isPaused) { time++; output.text(“Seconds: ” + time); } }, 1000); //with jquery $(‘.pause’).on(‘click’, function(e) { e.preventDefault(); isPaused = true; }); $(‘.play’).on(‘click’, function(e) { e.preventDefault(); isPaused = false; … Read more

Postponing functions in python

To execute a function after a delay or to repeat a function in given number of seconds using an event-loop (no threads), you could: Tkinter #!/usr/bin/env python from Tkinter import Tk def foo(): print(“timer went off!”) def countdown(n, bps, root): if n == 0: root.destroy() # exit mainloop else: print(n) root.after(1000 / bps, countdown, n … Read more

Do something every x minutes in Swift

var helloWorldTimer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: Selector(“sayHello”), userInfo: nil, repeats: true) func sayHello() { NSLog(“hello World”) } Remember to import Foundation. Swift 4: var helloWorldTimer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(ViewController.sayHello), userInfo: nil, repeats: true) @objc func sayHello() { NSLog(“hello World”) }

Jquery/Ajax call with timer

If you want to set something on a timer, you can use JavaScript’s setTimeout or setInterval methods: setTimeout ( expression, timeout ); setInterval ( expression, interval ); Where expression is a function and timeout and interval are integers in milliseconds. setTimeout runs the timer once and runs the expression once whereas setInterval will run the … Read more