admin管理员组文章数量:1410717
I am new to jquery and javascript . I need to set up a variable in javascript which is incrementing by 1 after each second . For that I did following :
function run(){
timer++;
}// run ends here
setInterval(run,1000);
Once the variable value is > 5, I want to enable code such that, whenever somebody hovers over iframe in html page, ajax request is done .
After sinigle ajax request I want to reset timer= 0.
if(timer>5){
$("iframe").hover(function(){
$.ajax({
url: 'http://localhost/test.html',
cache: false,
data: 'html',
success: function(data,status) {
}
});
});
timer=0;
}
This process should repeat again, counter should again start from 0 to 5 and ajax request functionality should be activated again .
Below is the plete code in one place :
<script>
var i = 0;
var timer=0;
function run(){
timer++;
}// run ends here
setInterval(run,1000);
if(timer>5){
$("iframe").hover(function(){
$.ajax({
url: 'http://localhost/test.html',
cache: false,
data: 'html',
success: function(data,status) {
}
});
});
timer=0;
}
</script>
I tried a lot and googled a lot, but was not able to figure out the solution .
I am new to jquery and javascript . I need to set up a variable in javascript which is incrementing by 1 after each second . For that I did following :
function run(){
timer++;
}// run ends here
setInterval(run,1000);
Once the variable value is > 5, I want to enable code such that, whenever somebody hovers over iframe in html page, ajax request is done .
After sinigle ajax request I want to reset timer= 0.
if(timer>5){
$("iframe").hover(function(){
$.ajax({
url: 'http://localhost/test.html',
cache: false,
data: 'html',
success: function(data,status) {
}
});
});
timer=0;
}
This process should repeat again, counter should again start from 0 to 5 and ajax request functionality should be activated again .
Below is the plete code in one place :
<script>
var i = 0;
var timer=0;
function run(){
timer++;
}// run ends here
setInterval(run,1000);
if(timer>5){
$("iframe").hover(function(){
$.ajax({
url: 'http://localhost/test.html',
cache: false,
data: 'html',
success: function(data,status) {
}
});
});
timer=0;
}
</script>
I tried a lot and googled a lot, but was not able to figure out the solution .
Share Improve this question asked Jul 12, 2013 at 14:28 Kshitij JainKshitij Jain 1,8035 gold badges21 silver badges30 bronze badges 03 Answers
Reset to default 6Try this :
var timer=0;
function run(){
timer++;
if(timer == 5){
$("iframe").on('mouseenter', function(){
$.ajax({
url: 'http://localhost/test.html',
cache: false,
data: 'html',
success: function(data,status) {
timer=0;
$('iframe').off('mouseenter')
}
});
});
}
}// run ends here
setInterval(run,1000);
If you already have mouseenter event on you iframe, doing .off('mouseenter')
will delete those binding.
As Ian suggested, you can name you event allowing you to unbind specifique binding.
To do it, just use a dot when bind/unbinding:
$("iframe").on('mouseenter.timer',...)
$('iframe').off('mouseenter.timer')
Use function memoization to avoid global "timer" variables:
function run(){
run.timer = run.timer || 0;
return run.timer++;
} // run ends here
setInterval(run,1000);
And to act accordingly to the timer, run your handling from run()
, for example:
function run(){
run.timer = run.timer || 0;
run.timer = handleTimer(run.timer++);
} // run ends here
setInterval(run,1000);
function handleTimer(timer) {
if(timer > 5){
$("iframe").hover(function(){
$.ajax({
url: 'http://localhost/test.html',
cache: false,
data: 'html',
success: function(data,status) {
}
});
// And disable hover handler once executed
$("iframe").unbind("mouseenter mouseleave");
});
return 0;
}
return timer; // continue timer chaining
}
Put your if
statement in run()
and move timer=0
to the success
function of your AJAX call.
function run() {
timer ++;
if(timer == 5) {
//Your ajax here
}
}
Your AJAX success
should look like success: function(data, status) { timer = 0;}
In your AJAX success, you will want to unbind
your iframe
hover, and then rebind it when timer > 5
so that the hover functionality is removed when the timer is less than 5.
本文标签:
版权声明:本文标题:jquery - How to set constantly incrementing counter in javascript , to perform another operation when counter > certain v 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744796177a2625588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论