问题
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