I used the following javascript in the develop tab to see old Facebook messages. There are thousands of messages and it just keeps going. Loading one month of messages took 30 minutes. I only need messages for the last year, but it looks like it will keep loading until it hits our first messages in 2010! I don't know how to make it stop without losing everything that it has already loaded. Do you know how I can stop the loop from executing?
setInterval(function () {
document.getElementById('see_older')
.getElementsByClassName('content')[0].click();
}, 500);
you should declare a variable for that interval function and then you can use clearinterval(theVarYouDeclared) to stop it
If you want it to stop right now, and don't want to refresh the page, a 'dirty' simple solution would be to open you browsers' console and execute the following code:
for (var i = 1; i < 99999; i++)
window.clearInterval(i);
Most major browsers will assign a small number as the ID so just looping over all possible numbers should work. But as mentioned, it would be better to store a reference to your interval in a variable so you could clear it using the clearInterval() method.
So I am creating an event to listen when there is a message and tell the time from when the last message that was sent. But I can't seem to figure out how to do just that. (I used message.author.lastMessage.createdTimestamp - new Date().getTime()) This does not seem to create a consistent time when I test it out. Any help on what's wrong or anything I need to fix would be appreciated
I recommend you to keep it simple and only use Dates.
// takes a User and returns the milliseconds of difference between now
// and the last message, if there's one.
function lastDiff({ lastMessage }) {
if (lastMessage) return new Date() - lastMessage.createdAt;
}
lastDiff(message.author);
Keep in mind that this can't always print the same number since milliseconds can change between a check and the next one.
Once you have the milliseconds, you can format them as you want.
I'm trying to use this https://codepen.io/zeinab92/pen/xwWGWM
But It's not updating 'Hours, Minutes and Status' automatically without page reload.
How can I make it working so, times will automatically update and state text will also update based on the time condition whether it's Open or Closed.
I like the script except the auto update problem, I tried by create a new function,
setInterval(function() {
$("#timeDiv").html(data);
}, 1000);
by disabling default
setInterval(checkTime, 1000);
or setTimeout instead of setInterval
But no luck.
After debugging, I found the problem with the code.
You have declared now as global variable outside checkTime function and when page loads now variable stores value of new Date i.e the value of Date when page has been loaded. Hence the value of now is updated only once as global variable are executed only once.
Solution: Place the now variable inside checkTime so that each time it will have new value of Date whenever checkTime is called.
function checkTime(){
var now=new Date();
....
}
Hope this resolves the issue. (JS Fiddle)
I am having trouble dynamically seeing the time update with moment js. It shows the correct time but I have to refresh my browser to get the time update. I would like it to update it in real time. Like momentjs.com main page.
I've tried using setInterval and setTimeout but for some reason I get the below digits that don't even update.
Here's what I have so far code-wise. Pretty simple as far as moment goes and all I want is seconds to keep counting...
update = ->
time = moment().format('hh:mm:ss a')
clock = setInterval update, 1000
console.log(clock) //output: 53296
Any ideas are immensely appreciated.
You should put the output inside of the update method and everthing will work as expected.
The method setInterval won't return the result of the repeatedly called method but an identifier which can be used with clearInterval to stop the execution.
Just a small working example to print the time every seconds and stop after 10 seconds:
update = ->
console.log(moment().format('hh:mm:ss a'))
x = setInterval update, 1000;
setTimeout (-> clearInterval(x)), 10000
If you want to use that time as content of some DOM-Element you can use the following code inside your update function (assuming you have an element (e.g. div) with id "time"):
JQuery:
$("#time").text(moment().format('hh:mm:ss a'))
Plain JS:
document.getElementById("time").firstChild.data = moment().format('hh:mm:ss a')
Try this. If u are dont have to use meomentJS.
https://github.com/furkankaynak/countdown
I'm having a hard time understanding the logic behind the setTimer method in javascript.
<html><head>
<script>
function Timer () {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
document.getElementById('show').innerHTML=h+":"+m+":"+s;
t = setTimeout("Timer()", 1000);
}
</script>
</head>
<body onload="Timer()">
<div id="show"></div>
</body></html>
setTimeout is used to delay a function/method execution. Then why it is being used in a real-time clock?
t = setTimeout("Timer()", 1000);
This part is confusing.
The clock is recursively calling itself, after the elapsed period of time.
Making a real-time clock is impossible in JS.
Because of how JS engines work, if you put Timer in a loop, to run for an infinite period of time, you'd never see the time update on the screen (as changes aren't drawn to the window until a function finishes and there's a gap in the program).
Also, inside that infinite-loop, it would be impossible to do anything else with the page (even closing it), because JS can only do one thing at a time, so it can't listen to any of the user's clicking until it's done with this loop.......
So that's what the setTimeout is for.
Timer is the function which acts as the clock.
Inside of the Timer function, at the end when all of the work is done, it's telling setTimeout to wait 1 second (1000ms) and then to call a function called Timer.
Timer just so happens to be the same function. But setTimeout doesn't know that, and doesn't care.
The t in this case is largely useless. setTimeout will return a number -- like taking a number at the doctor's office.
If, before you go through with it, you decide to back out, you can call clearTimeout(t); and it'll skip over that call (in this case, it would stop calling the clock).
There are a few bad-practices in here, that I figure I should mention, so that you can try not to copy them in your own practice.
First:
Pass setTimeout a reference to a function, and not a string...
var runThisFunction = function () { console.log("It's the future!"); },
time_to_wait = 250;
// DON'T DO THIS
setTimeout( "runThisFunction()", 250 );
// DO THIS
setTimeout( runThisFunction, 250 );
The difference is that setTimeout will run that string through eval, which can be a huge security concern depending on what you're trying to do.
The second problem is setting a random global variable, t... ...and hoping to use that as a solution.
First, in a couple of years, JS engines are going to start yelling at people for doing that stuff. Second, it's a huge hole, because any part of any app on that page could then overwrite t, or you could be relying on t somewhere else in your script, but every 1000ms, it gets written over with a new number.
Instead, they probably should have used a Timer.start(); and Timer.stop(); setup.
Your code:
t = setTimeout("Timer()", 1000);
The first thing you should know is that it's considered bad practice to put the first parameter in a string -- it should be the function name, unquoted, and without brackets, like so:
t = setTimeout(Timer, 1000);
That aside, your question about why it's being used to display a clock:
The use of setTimeout() inside the Timer() function to call itself is a common Javascript pattern to get a function to be called repeatedly. setTimeout() itself only triggers the function to be called a single time, after the given period of time has elapsed, so for a repeating event it needs to be re-triggered every time.
Since the setTimeout call is inside the Timer() function, it won't be set until Timer() is called the first time by some other means. This is where the body onload comes in.
As you suspect, setTimeout() isn't an accurate method for guaranteeing that a function will be called after exactly a given amount of time. Javascript is not multi-threaded, so any event handlers that are triggered must wait for any other code that is running at the same time. If something else is running slowly, this may cause your timer not to be triggered at exactly the moment it wants to be.
However, this isn't really a problem for your clock , because the clock is setting itself to the actual system time rather than relying on the setTimeout loop to keep itself in sync; the setTimeout loop is simply being used to make sure the display is updated (approximately) once a second. If it isn't actually quite exactly once a second, it doesn't really matter.
I hope that helps explain things a bit better.
When the Timer() function is called, it schedules itself to be run again one second later. The end result is once every second, Timer() updates the show element with the current time. (I have no idea why it's assigned to t, unless t is used in some other code on the page.)
The line starts The function again after one second.