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

  • SEARCH
  • Home
  • 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 8999681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:12:44+00:00 2026-06-16T00:12:44+00:00

I have a search input that listens to keyup and change to trigger an

  • 0

I have a search input that listens to keyup and change to trigger an update of a listview via Ajax.

Looks like this:

input.on('keyup change', function(e) {
    if (timer) {
        window.clearTimeout(timer);
    }
    timer = window.setTimeout( function() {
        timer = null;
        val = input.val();
        el = input.closest('ul');
        // run a function - triggers Ajax
        widget[func](dyn, el, lib_template, locale, val, "update");
    }, interval );
});

All working nice, except the handling of the timeout and binding, which causes double Ajax requests to be placed instead of a single one (when the keyup has passed, the change event triggers the same Ajax request again).

I can "fix" this by adding another timeout:

var runner = false;

input.on('keyup change', function(e) {
    if ( runner === false ){
        runner = true;
        if (timer) {
            window.clearTimeout(timer);
        }
        timer = window.setTimeout( function() {
            timer = null;
            val = input.val();
            el = input.closest('ul');
            widget[func](dyn, el, lib_template, locale, val, "update");
            // ssh....
            window.setTimeout( function(){ runner = false; },2500);
        }, interval );
    }
});

But this is not nice at all…

Question:
How can I make sure with two binding that both fire, that the function I need only runs once?

EDIT:
The Ajax call is triggered here:

widget[func](dyn, el, lib_template, locale, val, "update");

which calls this function to build a dynamic listview

buildListView : function( dyn,el,lib_template,locale,val,what ){
    ...
    // this calls my AJax Config "getUsers"
    $.parseJSON( dynoData[ dyn.method ](cbk, val, dyn.display) );

 });

 // config AJAX
 getUsers: function(cbk, val, recs){
  var form = "",
  pullRetailers = ( val === undefined ? "" : val ),
  service = "../services/some.cfc",
  method = "by",
  returnformat = "json",
  targetUrl = "",
  formdata = "...manually_serialized...,
  successHandler = function(objResponse, cbk) {
     cbk( objResponse );
  };
  // finally pass to the generic JSON handler
  ajaxFormSubmit( form, service, formdata, targetUrl, successHandler, "yes", "", returnformat, cbk );
}

// generic AJAX
var ajaxFormSubmit = 
    function ( form, service, formdata, targetUrl, successHandler, dataHandler, errorHandler, returnformat, type ){
    ...

    $.ajax({
        async: false,
        type: type == "" ? "get" : type,
        url: service,
        data: formdata,
        contentType: 'application/x-www-form-urlencoded',
        dataType: returnformat,
        success: function( objResponse ){
            if (objResponse.SUCCESS == true || typeof objResponse === "string" ){
                dataHandler == "yes" ? successHandler( objResponse, override ) : successHandler( override );
            }
        },  
        error: function (jqXHR, XMLHttpRequest, textStatus, errorThrown) { }
     });
}

But this does not help a lot regarding the actual question of how to prevent both events from triggering my Ajax Update.

  • 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-16T00:12:44+00:00Added an answer on June 16, 2026 at 12:12 am

    I would try to set up a value-checking function like this:

    var $inputIntance = $("#path-to-your-input");
    var lastInputValue;
    
    function checkInputValue () {
        var newValue = $inputIntance.val();
        if (newValue != lastInputValue) {
            // make your AJAX call here
            lastInputValue = newValue;
            el = $inputIntance.closest('ul');
            widget[func](dyn, el, lib_template, locale, lastInputValue, "update");
        }
    }
    

    and then then fire this checks by any user-action event you like:

    $inputIntance.on('keyup change', function(e) {
        checkInputValue();
    }
    

    or like this

    $inputIntance.on('keyup change', checkInputValue );
    

    UPDATE:
    there might be the case when you have to limit the number of AJAX requests per time.
    I added time control functionality to my previous code. You can find the code below and try it live here in JSFiddle.

    $(document).ready(function () {
        var $inputIntance = $("#test-input");
        var lastInputValue;
        var valueCheckTimer;
        var MIN_TIME_BETWEEN_REQUESTS = 100; //100ms
        var lastCheckWasAt = 0;
    
        function checkInputValue () {
            lastCheckWasAt = getTimeStamp();
            var newValue = $inputIntance.val();
            if (newValue != lastInputValue) {
                // make your AJAX call here
                lastInputValue = newValue;
                $("#output").append("<p>AJAX request on " + getTimeStamp() + "</p>");
                //el = $inputIntance.closest('ul');
                //widget[func](dyn, el, lib_template, locale, lastInputValue, "update");
            }
        }
    
        function getTimeStamp () {
            return (new Date()).getTime();
        }
    
        function checkInputValueScheduled() {
            if (valueCheckTimer) { // check is already planned: it will be performed in MIN_TIME_BETWEEN_REQUESTS
                return;
            } else { // no checks planned
                if  ((getTimeStamp() - lastCheckWasAt) > MIN_TIME_BETWEEN_REQUESTS) { // check was more than MIN_TIME_BETWEEN_REQUESTS ago
                    checkInputValue();
                } else { // check was not so much time ago - schedule new check in MIN_TIME_BETWEEN_REQUESTS
                    valueCheckTimer = window.setTimeout(
                        function () {
                            valueCheckTimer = null;
                            checkInputValue();
                        }, 
                        MIN_TIME_BETWEEN_REQUESTS
                    );
                }
            }
        }
    
        $inputIntance.bind('keyup change', function(e) {
            $("#output").append("<p>input event captured</p>");
            checkInputValueScheduled();
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form like this: <form name=serachForm method=post action=/home/search> <input type=text name=searchText
I would like to have a search box input field that flashes multiple default
I have a search form that should look like this: This is the HTML:
i have an input search like this: <input type=search autocomplete=off name=searchSchools id=searchSchools value= onKeyUp=searchSchools(this.value)
I have a search page like <div class=> <input id=search-input type=text class=input-medium search-query span4>
I have a search input box that appears upon rollover of a button. Rather
I have a live search input that shows results and lets you use the
I have an html input that sends the query string through an ajax call
I have a script that does an ajax request out on a keyup event
I have a search input text which I'd like to apply a focus() when

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.