How to execute the same javascript function on each page loaded with jquery mobile

kernelpanic

I need help to sort out this code, the goal is for each page loaded to show the array images for 3 seconds.

The first page works OK. The problem is that every page loaded after the first one the images change faster.

<div data-role="page" id="page1" class="banners">
 <div data-role="main" class="ui-content">
  <div class="patrocinadores" align="center">
      <img src="" width="100%" class="bannerPatrocinador">
  </div>
 </div>
</div>

<div data-role="page" id="page2" class="banners">
 <div data-role="main" class="ui-content">
  <div class="patrocinadores" align="center">
      <img src="" width="100%" class="bannerPatrocinador">
  </div>
 </div>
</div>

<div data-role="page" id="page3" class="banners">
 <div data-role="main" class="ui-content">
  <div class="patrocinadores" align="center">
      <img src="" width="100%" class="bannerPatrocinador">
  </div>
 </div>
</div>



<script>
    $('.banners').on("pagecreate",function(event){

      clearInterval(trocarImagem);

        let cmpBanner1 = localStorage.getItem("cmpBanner1");
        let cmpBanner2 = localStorage.getItem("cmpBanner2");
        let cmpBanner3 = localStorage.getItem("cmpBanner3");
        let cmpLink1 = localStorage.getItem("cmpLink1");
        let cmpLink2 = localStorage.getItem("cmpLink2");
        let cmpLink3 = localStorage.getItem("cmpLink3");

        let imagens = ["adm/img/mkt/"+cmpBanner1, "adm/img/mkt/"+cmpBanner2+"", "adm/img/mkt/"+cmpBanner3+""];
        let imagemAtual = 0;
        function trocarImagem(){
          imagemAtual = (imagemAtual + 1) % 3;
          var teste = $('.bannerPatrocinador').attr('src',imagens[imagemAtual]);
        }

       setInterval(trocarImagem, 3000);

    });
</script>

Gajotres

There are several problems in your code:

  1. clearInterval should accept setInterval as a variable, not a function you are calling through setInterval
  2. $('.bannerPatrocinador').attr('src', IMAGE); - This will add images to every bannerPatrocinador img box, not only on the active page
  3. It is not smart to reuse same class names in jquery Mobile application as everything is loaded into DOM so there's a good chance you will access multiple items at the same time.
  4. Forget jQuery Mobile, it's dead and no amount of necromancy will bring it back to life

Working example: http://jsfiddle.net/ws5p6nbk/

var interval;
var imagemAtual = 0;

function trocarImagem() {
  var imagens = ["https://s3.amazonaws.com/wmfeimages/wp-content/uploads/2018/09/27182802/4189366235_060e3e8e6f_z.jpg", "https://media.mnn.com/assets/images/2019/02/lexi.JPG.653x0_q80_crop-smart.jpg", "https://assets3.thrillist.com/v1/image/2709211/size/tmg-article_tall.jpg"];
  console.log(imagemAtual);
  imagemAtual = (imagemAtual + 1) % 3;
  var activePage = $.mobile.activePage;
  activePage.find('.bannerPatrocinador').attr('src', imagens[imagemAtual]);
}

$(document).on("pagecreate", '.banners', function(event) {

  clearInterval(interval);

  interval = setInterval(trocarImagem, 3000);

});

Please test these changes and accept the answer if you are satisfied.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Execute JQuery function when the page is loaded instead on .keyup

How to modify a jQuery Mobile page before it's loaded?

How to execute a function when page has fully loaded?

How to execute js / jquery code after php page has loaded

as in Javascript is not loaded into dom in jquery mobile

How to redirect to a page and then execute a jQuery function call

How to execute function on dynamically loaded javascript immediately after loading?

Get the loaded page in jQuery Mobile 1.4.2

Execute function in requested page when it is loaded

Cannot execute javascript code from loaded page

How to fire jQuery function on click event but only after the page is loaded?

Run Javascript function when jquery mobile page loads

How to use a JQuery function twice in the same page?

How to set same page transition for every navigation in jQuery mobile?

Execute function on clicking any element in a page except for two elements (in jQuery Mobile, jQM)

How to execute a function to update the values of dynamically created inputs after entire page has been loaded and rendered?

binding jquery function with form loaded after page

How to add and execute a JavaScript function to a web page with Watir

How can I execute a JavaScript function on the first page load?

How can I make the same function execute with javascript promises?

How to execute javascript files after files are loaded

How can I check if a jQuery loaded PHP file changes and execute function if changed?

Javascript function not working properly after page loaded

Using Jquery's .each() function makes the web page lag on mobile devices

How to post a form to same page loaded dynamically

How to get the same function to execute for each element of the same class one after the other?

Execute javascript when all videos on page have been fully loaded

Execute jQuery function each time scroll on div

How to play each audio element on a page (HTML, JQuery, Javascript)