Dan
Dan

Reputation: 2174

Jquery: how to automatically force a textbox to keyup in every 1 hour

I want to automatically fire the below function on every 1 hour. This is actually for currency converter, currently my below function takes action only if users enter/changes any value in the first textbox, *what i want is , below function should automatically call on every one hour, one hour timer time count should start when any changes happen in the textbox *

My function looks like this:

  $(document).ready(function(){ 
    $("#from_amount").keyup(function()
        {  
           //my currency converter function goes here

        ----------------------
        ----------------------

Upvotes: 0

Views: 494

Answers (4)

Johan
Johan

Reputation: 35223

If I understood you correctly:

function currencyConverter(e){
    //do stuff

    if(e && e instanceof jQuery.Event){
        //the function is triggered from the keyup event
    }
}

var handle = setInterval(currencyConverter, 3600000); //one hour in milliseconds

$(function(){
    $("#from_amount").keyup(currencyConverter);
});

If you want to stop the interval:

clearInterval(handle);

If you want to start the timer on a "new hour", you could to something like this (untested):

setInterval(function(){

    var now = new Date(),
        newHour = now.getMinutes() === 0 && now.getSeconds === 0;

    if(newHour){

        //trigger 1 hour interval
    }

}, 1000);

Upvotes: 2

edonbajrami
edonbajrami

Reputation: 2206

You can make a timer to trigger a function after a hour:

setTimeout(function() {
  // Do something after 1 hour
}, 3600000);

Upvotes: -1

shyam.y
shyam.y

Reputation: 1491

then use setInterval() function of javascript, like

setInterval(function currencyConverter(){
//your currency conversion code here
},3600000);

Upvotes: 0

Luke Pittman
Luke Pittman

Reputation: 843

An hour is a long time for this... but I've used the following in projects before:

window.setInterval(check_for_messages, 60000);

The number is the length of time in miliseconds, so this runs the "check_for_messages" function every 1 minute. The check_for_messages function is an ajax call that checks a database and updates a field on the website.

I don't know how much luck you'll have with 1 hour using this method though.

Upvotes: 0

Related Questions