开发者

JQuery function call with out an event

Suppose i have below jq function,

$(document).ready( 
  function(){ $('#demo_btn').click( 
         function(){ $.popup.show("The title", "A n开发者_运维问答ice message"); 
  } ); 
}); 

I need to call the above function with out a click event. For example suppose a i check a condition and if its > than 10 im showing the above message. So its like no click event but just calling the above function when a condition satisfy after the page load.

More details its like im set a value for a variable in the controller and from view tpl im acccess it. So if the variable is >10 im showing the above alert. So if the variable is >10 the above alert should be display with out a click on a buton.

Thanks.


use trigger.

<?php if ($var > 10) { ?>
    $('#demo_btn').trigger('click');
<?php } ?>


You can create a function in javascript if it satisfies the above condition then call the popup function.

if you can post the whole code then it will be easy to suggest an exact solution.


when you are using a php, you can use it like below

<script>
$(document).ready(function(){ 
  <?php if($variable>=10){?>
 $.popup.show("The title", "A nice message"); 
 <?php }?>
}); 
</script>


If I've understood your question correctly, you can do something like this:

$('#demo_btn').click(doStuff);

function doStuff() {
   $.popup.show("The title", "A nice message");
}

You can then call the doStuff function as normal anywhere else in your code.

Update... Having reread your question, I think you might actually be looking for something that is constantly checking to see if a variable has a certain value. If that's the case, you can use setInterval:

var x = 1;

setInterval(doStuff, 500);

function doStuff() {
    if(x > 10) {
        //Do stuff
    }
}

The above code checks the value of x to see if it's greater than 10 every 500 milliseconds. Here's an example fiddle showing the above concept in action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜