using jquery how to trigger event continually after the given time intervel

血红的双手。 提交于 2021-01-29 18:31:20

问题


I need to update given selector continually after the given time intervel. this is my code using javascript.

function current_time(){
   var content = document.getElementById('time_div');
   content.innerHTML = '<p> Time is : '+Date()+'</p>';
}

var v = setInterval( 'current_time()', 1000);

this is ok. but my question is using jquery can we do that ? I tried this way. it works only one time. how I trigger that event continually after the given time interval(without click event).

$(document).ready(function(){
    $('#time_div').click(function(){
        $(this).html('<p>'+Date()+'</p>');
    });
});

回答1:


This should do the trick...

$(document).ready(function() {
    var v = setInterval(function() {
        $("#time_div").html("<p>" + Date() + "</p>");
    }, 1000);
});

To make your original code a little friendlier to read...

var v = setInterval(function() {
   document.getElementById("time_div").innerHTML = "<p>" + Date() + "</p>";
}, 1000);



回答2:


All you need to do is this :-

$(document).ready(function(){
    $('#time_div').click(function(){
        $(this).html('<p>'+Date()+'</p>');
    });
});

var v = setInterval( function(){$('#time_div').trigger('click')} , 1000);

setInterval is a standard JavaScript function, not part of jQuery. You call it with a function to execute and a period in milliseconds. e.g:-

setInterval(function() {
//Do something every 1 seconds
}, 1000);



回答3:


Try this:

$(document).ready(function(){
    setInterval(function() {
                    $('#time_div').html('<p>'+Date()+'</p>');
                }, 1000);
});


来源:https://stackoverflow.com/questions/13526608/using-jquery-how-to-trigger-event-continually-after-the-given-time-intervel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!