简体   繁体   中英

Javascript functions being executed twice

I have this next code which I am trying to print some data on screen. The issue which I am facing is that each function is being executed twice so therefore the console.log appears on my console twice also.

It is really bizarre that this thing is happening. My code goes like this: I tried doing it with window.onload and also tried to import the functions inside the document.ready but no luck.

If anyone can give me any hint what I am doing wrong, that would be really appreciating.

<body class="stretched">
    <div id="wrapper" class="clearfix">
        <!--Cargamos Layout de NavBar -->
        @Html.Partial("_NavBar_DataUser")
        <!--Fin de Carga -->
        <section id="carouselComercialTop" class="mt-auto">
            <div class="content-wrap">
                <!--Cargamos el caroussel stático -->
                @Html.Partial("_Layout_Caroussel")
            </div>

        </section>
        @Html.Partial("_Layout_Medicina360")

        <iframe class="pt-4" frameborder="0" width="100%" height="100%" src='#' name="zoom360" id="zoom360" style="display:none;"></iframe>
    </div>
    @Html.Partial("_Layout_DataUserFooter")
    <script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script type="text/javascript" src="/lib/slick-1.8.1/slick/slick.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/lib/slick-1.8.1/slick/slick-theme.css" />
    <link rel="stylesheet" type="text/css" href="/lib/slick-1.8.1/slick/slick.css" />
</body>
    <script>
      window.ca_model = [];
      let stringJSONConsultaBuscador = ("@Context.Session.GetString("ConsultaBuscador")");
      window.ca_model = JSON.parse(stringJSONConsultaBuscador.replace(/&quot;/g, '"'));
      let especialistas = window.ca_model.listaEspecialistas;
      let especialista360varon = [];
      let especialista360mujer = [];
      let especialista360nino = [];
      let listaEspecialistas = window.ca_model.listaEspecialistas;
      let varon360 = listaEspecialistas.filter(b => b.hombres360 !== false);
      let mujer360 = listaEspecialistas.filter(b => b.mujeres360 !== false);
      let ninos360 = listaEspecialistas.filter(b => b.ninos360 !== false);

      especialista360varon.push(varon360);
      especialista360mujer.push(mujer360);
      especialista360nino.push(ninos360);

      especialista360varon = especialista360varon[0];
      especialista360mujer = especialista360mujer[0];
      especialista360nino = especialista360nino[0];

    function loadEspecialista360Varon(especialista360varon) {
        console.log(especialista360varon);
    }
    function loadEspecialista360Mujer(especialista360mujer) {
        console.log(especialista360mujer);
    }
    function loadEspecialista360Nino(especialista360nino) {
        console.log(especialista360nino);
    }

    window.onload = function () {
        loadEspecialista360Varon(especialista360varon);
        loadEspecialista360Mujer(especialista360mujer);
        loadEspecialista360Nino(especialista360nino);
    };
    $(document).ready(function () {
        let mobile = window.matchMedia('(max-width: 767px)').matches;
        let url = window.location.href;

        $('.link-carousel').each(function(){
            let href = $(this).attr("href");
            let container = $(this).parent().parent();
            if (url.indexOf(href) > -1){
                container.css({"background-color" : "rgb(173, 124, 177)"});
            }
        });

        let cuadroMedicoButton = document.getElementById("cuadroMedicoButton");
        cuadroMedicoButton.setAttribute("href", "/CuadroAsistencial/AsistenciaSpecialistas");
        if (mobile) {
            $('#mainRow').removeClass("d-flex");
            $('#mainCol').removeClass("d-flex");
            $('#containerMedicina360').replaceClass("py-5 py-2");
        }
    });
</script>

Well the solution to the answer was to remove the src='#' from here.

    <iframe class="pt-4" frameborder="0" width="100%" height="100%" src='' name="zoom360" id="zoom360" style="display:none;"></iframe>

It is loading the same page on parent window and inside the same iframe, that's why it looks like the functions where being called twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM