How to use the `PerformanceNavigationTiming` API to get page load time?

I am trying to to use the PerformanceNavigationTiming API to generate a page load metric.

The MDN API document linked above says that the PerformanceEntry.duration should give me what I need because it:

[r]eturns a timestamp that is the difference between the PerformanceNavigationTiming.loadEventEnd and PerformanceEntry.startTime properties.

However, when I check this property, I get simply 0. I’m accessing this API from within a React hook that runs a useEffect function that wait for the window load event and then checks the api like so:

export const useReportPageLoadTime = () => {
  useEffect(() => {
    const reportTime = () => {
      let navPerformance: PerformanceEntry
      navPerformance = window.performance.getEntriesByType('navigation')[0]

      console.log({
        duration: navPerformance.duration,
        blob: navPerformance.toJSON()
      })
    }

    if (document.readyState === 'complete') {
      reportTime()
      return null
    } else {
      window.addEventListener('load', reportTime)
      return () => window.removeEventListener('load', reportTime)
    }
  }, [])
}

As you can see there, I also call toJSON on the performance entry and indeed it shows that the values upon which duration (startTime and loadEventEnd) are both 0 as well:

enter image description here

Does anyone know why I am getting this value?

how to filter and clear data by using JavaScript?

see image for some clarification
I am getting error in filter function and clear function in JavaScript code

I have a students data(subject and marks). I want to filter students data by marks(above, below or between specific range) by clicking filter button and If I click clear button the filter data should be cleared and it returns all students data.

This is my html code

<body onload="event.preventDefault();onPageLoad()">
    <h1>Student Details</h1>
    <div>
        <label>Subject</label>
        <select id="subjects">
            <option id="opt0" value="">-- Select Subject --</option>
            <option id="opt1" value="English">English</option>
            <option id="opt2" value="Maths">Maths</option>
            <option id="opt3" value="Science">Science</option>
            <option id="opt4" value="SocialScience">Social Science</option>
        </select>
        <label>Filter By</label>
        <input type="radio" id="above" name="mode" value="above" onclick="filterBy()" checked />
        <label for="above">above</label>
        <input type="radio" id="below" name="mode" value="below" onclick="filterBy()" />
        <label for="below">below</label>
        <input type="radio" id="between" name="mode" value="between" onclick="filterBy()" />
        <label for="between">between</label>
        <input id="mark" type="number">
        <label id="to" style="display: none;">to</label>
        <input id="maxMark" type="number" style="display: none;" id="inputMark">
        <button id="filter" onclick="filterClick()">Filter</button>
        <button id="clear" onclick="Clear()">Clear</button>
    </div>
    <table id="studentTable">
        <thead>
            <tr>
                <th>S.No</th>
                <th>Name</th>
                <th>English</th>
                <th>Maths</th>
                <th>Science</th>
                <th>Social Science</th>
            </tr>
        </thead>
        <tbody id="studentData">
        </tbody>
    </table>
</body>

I am getting error in these javascrpt functions

function filterBy() {
    // code goes here to select filter by option
   var modes = document.getElementsByName("mode");
    for(var i = 0; i < modes.length; i++){
        if(modes[i].checked){
            mode_value = modes[i].value;
        }
    } 
    if(mode_value == "between") {
        document.getElementById("to").style.display = '';
        document.getElementById("maxMark").style.display = '';
    } else {
        document.getElementById("to").style.display = 'none';
        document.getElementById("maxMark").style.display = 'none';
    } 
}
function Clear() {
    // code goes here to clear filter
  document.getElementById("mark").value = '';
  document.getElementById("subjects").value = ''; 
  var input, filter, table, tr, td, i;
  input = document.getElementById("clear");
  filter = input.value.toUpperCase();
  table = document.getElementById("studentTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }       
  }
}
function filterClick() {
    // code goes here to handle filter button    
    tr = table.getElementsByTagName("tr");
    input = document.getElementById("mark");
    input1 = document.getElementById("maxMark");
    subjectOption_value = document.getElementById("subjects").value;
    subjectOption_index = document.getElementById("subjects").selectedIndex;
    if(subjectOption_value != "") {
        for (i = 0; i < tr.length; i++) {
            cells = tr.item(i).cells;
            cellVal = cells.item(subjectOption_index+1).innerHTML;         
            if (mode_value == 'above') {
                if(cellVal > input.value) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }                    
            } else if (mode_value == 'below') {
                if(cellVal < input.value) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            } else if(mode_value == 'between') {
                if(cellVal >= input.value && cellVal < input1.value) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }

Firebase Webpage different from localhost

Sorry if anything I say is dumb or whatever, I’m just starting out

So I’m starting to creating a React.js webpage hosted on firebase and to start out I have been creating a component that is just a dynamic text and button together. The code for this component in my counter.jsx file is

import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 1,
  };

  styles = {
    fontSize: 20,
    fontWeight: "bold",
  };

  render() {
    // const { imageUrl } = this.state;
    return (
      <div>
        <span className={this.getBadgeClasses()}>{this.formatCount()}</span>
        <button className="btn btn-secondary btn-sm">Increment</button>
      </div>
    );
  }

  getBadgeClasses() {
    let classes = "badge m-2 bg-";
    classes += this.state.count === 0 ? "warning" : "primary";
    return classes;
  }

  formatCount() {
    const { count } = this.state;
    return count === 0 ? "Zero" : count;
  }
}

export default Counter;

This code looks like this when ran from my localhost:3000 npm start deal. (Does not display index.html contents, only react components) Button and text

The code that renders this and all within my index.js is

import React from "react";
import ReactDOM from "react-dom";
import './index.css'
import 'bootstrap/dist/css/bootstrap.css'
import Counter from './Components/counter'

ReactDOM.render(<Counter />, document.getElementById('root'));

So far, everything is fine when it is ran from my computer, but when this is deployed to my firebase web project (under the free plan) it solely displays my index.html file rather than the react rendered component (meaning its just the html text, not button and stuff). Below is my index.html file that is displyed on my hosted webpage from firebase, but is never shown on my localhost:3000

<!DOCTYPE html>
<html lang="en">
  <div id="root">
    <h1>this is html, not react component</h1>
  </div>
</html>

Is there something I’m missing as to why my firebase webpage is different than my localhost? Again, I’m just starting out so any help is much appreciated

DOMHighResTimeStamp in Safari 14.1.2 on OSX 10.15.6 impossibly large

I was looking through some data and have found an issue with the values of DOMHighResTimeStamp – either from performance.now() or event.timeStamp. For all 200+ people, with a range of browsers and systems, recorded times are in a reasonable range of around 4000. However for 4 individuals, recorded reaction times for all trials were integers 12+ digits long (e.g. 24158598587397500 for one of them) – obviously impossible.

3/4 of these people had the following user agent:

  • Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15

i.e. Safari 14.1.2, OSX 10.15.6. There are others with the this OS or same operating system whose results are fine, but none with this specific combination. Is anyone aware of what happened here, or how to mitigate this in future? Normal polyfills won’t work as the function appears to be returning something and so is found by the browser.

Also, the fourth person was using:

  • Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko)

i.e. Apple Mail 605.1.15, exact same OS

Can’t download PDF on mobile/android app Using jsPDF

I am using a jsPDF library for creating PDFs and downloading them. It is working on a mobile browser but in the mobile app, it is not working anymore. I searched that the doc.save() function is not functioning on the mobile app. is There any way to replace for save() function to work the download pdf on the mobile/android app?

How can i make a website that if we scroll down the X position move to the right / horizontal scroll

<!---Java--->
    <script>
        let container = document.getElementById('container');

        window.addEventListener('scroll',function(){
            let value = window.scrollY;
            container.style.right = value * 0.25 + 'px';
        })
    </script>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.container-top,
.container-bottom {
    position: relative;
    display: flex;
    width: 100%;
    scroll-behavior: smooth;
}

.container {
    position: relative;
    display: flex;
    width: 100%;
    scroll-behavior: smooth;
    right: 0;
}

.container section,
.container-top,
.container-bottom {
    display: flex;
    justify-content: center;
    align-items: center;
    min-width: 100%;
    height: 100vh;
    background: #f53b62;
}

.container section:nth-child(2) {
    background: #505050 ;
}

.container section:nth-child(3) {
    background: #46c2a1 ;
}

.container section:nth-child(4) {
    background: #f49c61 ;
}

.content{
    max-width: 800px;
    text-align: center;
    padding: 40px;
}

.content h2 {
    text-transform: uppercase;
    font-size: 40px;
}

.content p {
    color: aliceblue;
    font-size: 20px;
    line-height: 30px;
}

.container-top {
    background: #8662f5;
}

.container-bottom {
    background: #6296f5;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>scroll horinzontal</title>
</head>
<body>

    <div class="container-top">
        <section>
            <div class="content">
                <h2>Zero</h2>
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Voluptate et ipsum illo ipsa mollitia, 
                    voluptatum rem quidem neque? Aut dolore ex cum perspiciatis non quidem placeat deleniti dolorum 
                    corporis assumenda?</p>
            </div>
        </section>
    </div>

    <div class="container element_size" id="container" >
        <section>
            <div class="content">
                <h2>One</h2>
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Voluptate et ipsum illo ipsa mollitia, 
                    voluptatum rem quidem neque? Aut dolore ex cum perspiciatis non quidem placeat deleniti dolorum 
                    corporis assumenda?</p>
            </div>
        </section>
        <section>
            <div class="content">
                <h2>Two</h2>
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Voluptate et ipsum illo ipsa mollitia, 
                    voluptatum rem quidem neque? Aut dolore ex cum perspiciatis non quidem placeat deleniti dolorum 
                    corporis assumenda?</p>
            </div>
        </section>
        <section>
            <div class="content">
                <h2>Three</h2>
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Voluptate et ipsum illo ipsa mollitia, 
                    voluptatum rem quidem neque? Aut dolore ex cum perspiciatis non quidem placeat deleniti dolorum 
                    corporis assumenda?</p>
            </div>
        </section>
        <section>
            <div class="content">
                <h2>Four</h2>
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Voluptate et ipsum illo ipsa mollitia, 
                    voluptatum rem quidem neque? Aut dolore ex cum perspiciatis non quidem placeat deleniti dolorum 
                    corporis assumenda?</p>
            </div>
        </section>
    </div>

    <div class="container-bottom">
        <section>
            <div class="content">
                <h2>Five</h2>
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Voluptate et ipsum illo ipsa mollitia, 
                    voluptatum rem quidem neque? Aut dolore ex cum perspiciatis non quidem placeat deleniti dolorum 
                    corporis assumenda?</p>
            </div>
        </section>
    </div>

</body>
</html>

Horizontal and Vertical scrolling

I want to make a simple website like burgerboss.com scroll effect , when we scroll until y position and we continue scroll down the page scroll to the horizontal scroll position without scrollbar help and if we scroll in the end of the horizontal content it continue scroll down

I have try to reseacrh about this problem but i cant find one

What if I want this image magnifier to be disabled on default then I will just enable it to start zooming in the image?

This is the link for the script and styles:

<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://thdoan.github.io/magnify/css/magnify.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://thdoan.github.io/magnify/js/jquery.magnify.js"></script>

This is the image and button to disable the magnifier, I put it inside the body:

<img id="sample-watch" src="https://i.imgur.com/jnRTmfe.jpg" alt="Sample image" class="zoom" 
data-magnify-src="https://i.imgur.com/z9yzdPO.jpg"> &nbsp; &nbsp; &nbsp;

<a href="https://i.imgur.com/n1xHDD4.jpg">
    <img id="sample-glasses" src="https://i.imgur.com/55Hondr.jpg" alt="Sample image" 
    class="zoom">
</a>

<p><br><button id="toggle-zoom">Disable Magnifier</button><br><br></p>

Here’s the script, it automatically enabled because of the ready(function), I changed it to click(function) but the disable doesn’t run:

<script>
    var $zoom;
    $(document).ready(function() {

        $zoom = $('.zoom').magnify({
            afterLoad: function() {

            }
        });


        $('#toggle-zoom').click(function() {
            if ($(this).hasClass('zoom-disabled')) {
                $zoom = $('.zoom').magnify({
                    afterLoad: function() {

                    }
                });
                $(this).text('Enable Magnifier').removeClass('zoom-disabled');
            } else {
                $zoom.destroy();
                $(this).text('Disable Magnifier').addClass('zoom-disabled')
            }
        });


    });
</script>

How to get main url of project in reactjs

I am working with reactjs and i want to get fullpath/hostname/baseurl of my project (inside component file),How can i do this ?I tried with following code,but not working,giving me following error

"Unexpected token `{`. Expected * for generator"

I tried with following code

import absoluteUrl from 'next-absolute-url';
class Header extends Component {
const { origin } = absoluteUrl(req)
const apiURL = `${origin}
render() 
    {
        ...//my code
    }   
}

How to copy specific values of array to another array in Javascript?

I have 2 arrays like this:

const arr1 = [
    {id: 1, qty: 2, checked: true},
    {id: 2, qty: 2, checked: true},
    {id: 3, qty: 2, checked: false}
]

const arr2 = [
    {id: 1, qty: 2},
    {id: 2, qty: 2}
]

I want to copy values of arr1 to arr2 where checked is true only, more than that if arr1 value is already exist in arr2 I just want it to update its qty not duplicate again. But my problem is that in some scenarios I got it duplicate and update at the same time. Below here I try with for loops.

const handleAdd = () => {
    let newArray = [...arr2]
    for (let i = 0; i < arr1.length; i++) {
        for (let j = 0; j < arr2.length; j++) {
            if(arr2[j].id === arr1[i].id && arr1[i].checked === true){
                newArray[j].qty = newArray[j].qty + arr1[i].qty
                break
            }else{
                if(arr1[i].checked === true){
                    newArray.push(arr1[i])
                    break
                }
            }
        }
    }
    console.log(newArray)
}

What went wrong here, any solution? Thanks in advance

The below mentioned syntax seems logically wrong? (for loop -JavaScript) [duplicate]

i = 0;
for( ;i<3; ){
  alert(i++);
}

The above code should output a ‘1’ after the first iteration as ‘i’ has been initialized as 0. Instead, the first alert brings up a ‘0’. How is this logically correct as the first output should’ve been the incremented value itself?alert(i++);

This is syntactically equal to the code

for (let i=0; i<3; i++)
{
    alert(i);
}