setTimeout causes page load bug after sign up or login
When a user signs up, the home page gets loaded (student-home.handlebars);
If a navigate to the messages page i have set a timeout on the update compile because i have to wait for firebase to load in the messages.
However, this SetTimeout causes a bug: when i sign up, the homepage renders and in one second (after the setTimeout of 1000ms) i get automatically navigated to student-messages.handlebars.
This only happens with this timeOut being set, but should actually not be possible because the student-home.handlebars doesn't read any code from the student-messsages.handlebars.
Is there another way to load the message view with a small delay so the messages are being showed? Or an explanation why this setTimeout causes this problem?
import {
getInstance
} from '../firebase/firebase';
const firebase = getInstance();
const studentMessagesViewTemplate = require('../templates/student-messages.handlebars');
export default () => {
const database = firebase.database();
const ref =
database.ref('messages/').orderByChild('receiver').equalTo(currentUserKey);
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
});
setTimeout(() => {
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
}, 1000);
}
javascript firebase handlebars.js
add a comment |
When a user signs up, the home page gets loaded (student-home.handlebars);
If a navigate to the messages page i have set a timeout on the update compile because i have to wait for firebase to load in the messages.
However, this SetTimeout causes a bug: when i sign up, the homepage renders and in one second (after the setTimeout of 1000ms) i get automatically navigated to student-messages.handlebars.
This only happens with this timeOut being set, but should actually not be possible because the student-home.handlebars doesn't read any code from the student-messsages.handlebars.
Is there another way to load the message view with a small delay so the messages are being showed? Or an explanation why this setTimeout causes this problem?
import {
getInstance
} from '../firebase/firebase';
const firebase = getInstance();
const studentMessagesViewTemplate = require('../templates/student-messages.handlebars');
export default () => {
const database = firebase.database();
const ref =
database.ref('messages/').orderByChild('receiver').equalTo(currentUserKey);
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
});
setTimeout(() => {
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
}, 1000);
}
javascript firebase handlebars.js
add a comment |
When a user signs up, the home page gets loaded (student-home.handlebars);
If a navigate to the messages page i have set a timeout on the update compile because i have to wait for firebase to load in the messages.
However, this SetTimeout causes a bug: when i sign up, the homepage renders and in one second (after the setTimeout of 1000ms) i get automatically navigated to student-messages.handlebars.
This only happens with this timeOut being set, but should actually not be possible because the student-home.handlebars doesn't read any code from the student-messsages.handlebars.
Is there another way to load the message view with a small delay so the messages are being showed? Or an explanation why this setTimeout causes this problem?
import {
getInstance
} from '../firebase/firebase';
const firebase = getInstance();
const studentMessagesViewTemplate = require('../templates/student-messages.handlebars');
export default () => {
const database = firebase.database();
const ref =
database.ref('messages/').orderByChild('receiver').equalTo(currentUserKey);
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
});
setTimeout(() => {
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
}, 1000);
}
javascript firebase handlebars.js
When a user signs up, the home page gets loaded (student-home.handlebars);
If a navigate to the messages page i have set a timeout on the update compile because i have to wait for firebase to load in the messages.
However, this SetTimeout causes a bug: when i sign up, the homepage renders and in one second (after the setTimeout of 1000ms) i get automatically navigated to student-messages.handlebars.
This only happens with this timeOut being set, but should actually not be possible because the student-home.handlebars doesn't read any code from the student-messsages.handlebars.
Is there another way to load the message view with a small delay so the messages are being showed? Or an explanation why this setTimeout causes this problem?
import {
getInstance
} from '../firebase/firebase';
const firebase = getInstance();
const studentMessagesViewTemplate = require('../templates/student-messages.handlebars');
export default () => {
const database = firebase.database();
const ref =
database.ref('messages/').orderByChild('receiver').equalTo(currentUserKey);
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
});
setTimeout(() => {
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
}, 1000);
}
javascript firebase handlebars.js
javascript firebase handlebars.js
asked Jan 2 at 10:33
brunellibrunelli
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A setTimeout()
is only the proper solution when you want something to run after a certain time. Here you want to run the code after the data is loaded, which is not a certain time (it is in fact pretty uncertain most of the time).
To run your other code after the data has loaded, move it into (or call it from) the callback that Firebase invokes after the data has loaded. Something like this:
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004749%2fsettimeout-causes-page-load-bug-after-sign-up-or-login%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A setTimeout()
is only the proper solution when you want something to run after a certain time. Here you want to run the code after the data is loaded, which is not a certain time (it is in fact pretty uncertain most of the time).
To run your other code after the data has loaded, move it into (or call it from) the callback that Firebase invokes after the data has loaded. Something like this:
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
});
add a comment |
A setTimeout()
is only the proper solution when you want something to run after a certain time. Here you want to run the code after the data is loaded, which is not a certain time (it is in fact pretty uncertain most of the time).
To run your other code after the data has loaded, move it into (or call it from) the callback that Firebase invokes after the data has loaded. Something like this:
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
});
add a comment |
A setTimeout()
is only the proper solution when you want something to run after a certain time. Here you want to run the code after the data is loaded, which is not a certain time (it is in fact pretty uncertain most of the time).
To run your other code after the data has loaded, move it into (or call it from) the callback that Firebase invokes after the data has loaded. Something like this:
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
});
A setTimeout()
is only the proper solution when you want something to run after a certain time. Here you want to run the code after the data is loaded, which is not a certain time (it is in fact pretty uncertain most of the time).
To run your other code after the data has loaded, move it into (or call it from) the callback that Firebase invokes after the data has loaded. Something like this:
ref.on("value", function (snap) {
snap.forEach(function (childSnapshot) {
let data = childSnapshot.val();
if (data.receiver === currentUserKey) {
Message = {
content: data.content,
sender: data.senderName,
senderKey: data.senderKey,
receiver: currentUserKey,
date: data.date
}
}
messageList.push(Message);
// console.log(messageList);
});
// Return the compiled template to the router
update(compile(studentMessagesViewTemplate)({
messageList
}));
let messageDetail = document.querySelectorAll('.messages-list');
for (let i = 0; i < messageDetail.length; i++) {
messageDetail[i].id = "messageDetail" + i;
messageDetail[i].addEventListener('click', showDetail);
};
// firebase logout at buttonclick
const btnLogout = document.querySelector('.btnLogout');
btnLogout.addEventListener('click', e => {
firebase.auth().signOut().then(function () {
window.location.replace('#/');
});
});
});
answered Jan 2 at 15:24
Frank van PuffelenFrank van Puffelen
240k29383410
240k29383410
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004749%2fsettimeout-causes-page-load-bug-after-sign-up-or-login%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown