Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:54:10+00:00 2026-06-17T17:54:10+00:00

I´m trying to make a script with this goals: Check every X seconds if

  • 0

I´m trying to make a script with this goals:

  1. Check every X seconds if there is a new order ( function refreshIntervalId() )
  2. if there is a new order, stop the setTimeover
  3. perform some actions in another function ( function refreshIntervalId2() ) and start de setTimeover again

I achieve that with this code, it works but the timer never stops and it keeps executing every 5 seconds.

Do you know how can I solve this?
Thanks in advance

  var refreshIntervalId, refreshIntervalId2;

  $("input:text:visible:first").focus();

  refreshIntervalId2 = (function() {
    return {
      update: function() {
        var pedidoid, url;
        clearInterval(refreshIntervalId);
        pedidoid = $("#pedidoid").val();
        url = Routing.generate("get_pedido", {
          id: pedidoid
        });
        return $.getJSON(url, function(json) {
          clearInterval(refreshIntervalId2);
          if (json.entregado === 1) {
            return location.reload(true);
          }
        });
      }
    };
  })();

  refreshIntervalId = (function() {
    return {
      update: function() {
        var url;
        url = Routing.generate("get_pedidos");
        return $.getJSON(url, function(json) {
          var imgsrc;
          clearInterval(refreshIntervalId);
          $(".hero-unit").children("h1").text("Pedido nuevo!");
          $(".hero-unit").children("h2").text("");
          imgsrc = "/images/uploads/" + json.pedido.material.foto;
          $("#imagenpedidomaterial").attr("src", imgsrc);
          $(".cajapuesto").css({
            position: "absolute",
            top: json.pedido.puesto.y + "px",
            left: json.pedido.puesto.x + "px"
          });
          $(".cajapuesto").addClass(json.kolorea);
          $("#divimagenmaterial").show("slow");
          $("#divcodbar").show("slow");
          $("#pedidoid").val(json.pedido.id);
          return setInterval(refreshIntervalId2.update, 5000);
        });
      }
    };
  })();

  $(document).ready(function() {
    return setInterval(refreshIntervalId.update, 5000);
  });

  $(document.body).on("keypress", function(e) {
    var $micodbar, url;
    switch (e.which) {
      case 13:
        $("#divmaterialincorrecto").hide();
        $micodbar = $("#checkcodbar").val();
        url = Routing.generate("get_material", {
          codbar: $micodbar
        });
        return $.ajax(url, {
          type: "GET",
          contentType: "application/json",
          success: function(data) {
            var datos;
            if (data === "null") {
              return $("#divmaterialincorrecto").show("slow");
            } else {
              datos = jQuery.parseJSON(data);
              return $(".row").show("slow");
            }
          },
          error: function(xhr, ajaxOptions, thrownError) {
            console.log("Errorea");
            alert(xhr.status);
            return alert(thrownError);
          }
        });
    }
  });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:54:11+00:00Added an answer on June 17, 2026 at 5:54 pm

    It’s because refreshIntervalId and refreshIntervalId2 are NOT references to your timers, but are references to objects that contain a method update which I suppose are namespaces in purpose.

    clearInterval needs a timer as an argument and these can be acquired from the return value of a setInterval call.

    You must satisfy this form of code in order to reference your timers for future clearing:

    //myTimer is a reference to your timer created by setInterval.
    var myTimer = setInterval(func,interval);
    
    //to clear myTimer, pass it to clearInterval
    clearInterval(myTimer);
    

    Something like this structure should suffice:

    $(document).ready(function() {
      var firstInterval
        , secondInterval
        ;
    
      //if you are not creating local scopes, I suggest ditching the IFFE
      //and go for a literal object instead.
      var refreshIntervalId = {
        update : function(){
          ...
          return $.getJSON(url, function(json) {
            ...
            clearInterval(firstInterval);
            ...
          });
          ...
        }
      };
    
      //run refreshIntervalId.update every 5 seconds
      //timer referenced by firstInterval
      firstInterval = setInterval(refreshIntervalId.update, 5000);
    });
    

    Just an aside, your keypress handler should be in $(document).ready() to make sure DOM elements are loaded before they are operated on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to make this script from ClearBox3 to stop conflicting with MooTools
I am trying to make a script to login into my check card balance
I'm trying to make simple script using jquery $post function to pass data to
I'm trying to make a filter on script to make this happen: Before: 123.125.66.126
I'm trying to make this script for a forum, which allows the use of
I am trying to make this script work but it's... giving me indentation errors
I'm trying to make a HTTP request from a Adobe Illustrator Script (this question
I am trying to make a call with this script, but am getting a
Trying to make this script go random. I tried variations of this string $banner_no
i am trying to make this script that filters a line coming from ps..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.