简体   繁体   English

如何使该功能只运行一次?

[英]How to make so that the function run only one time?

Script number one performed for each of a given set of elements DOM, append to the first element of the DOM script number two.为给定元素 DOM 中的每一个执行的第一个脚本,附加到第二个 DOM 脚本的第一个元素。

Script number one:脚本一:

function ptmedia(){
$('.ptmcss').eq(0).html('<div class="ptmcss"></div>')
.append('<div class="append"><script type="text/javascript" src="script2.js"></script></div>');
$('.spbin').each(function(){
if($(this).children().attr('class').indexOf('ptmd')!==-1){
$(this).addClass('ptmedia');ptmedia();}

Script number two does cross domain query, the fulfillment of which performs the specified function (z) .第二个脚本执行跨域查询,其执行执行指定的函数(z) Script number two :脚本二

$.getJSON('http://example.com?callback=?',function(z){alert(z.query+'Some text');}

Due to the fact that the script is the number one multiple times, the function (z) holds an equal number of times.由于脚本多次成为第一,因此函数(z)保持相同的次数。 How to make so that the function (z) run only one time?如何使函数(z)只运行一次? Thank's for any help!谢谢你的帮助!

Are you looking for the jquery .one statement?您在寻找 jquery .one语句吗? http://api.jquery.com/one/ http://api.jquery.com/one/

Check if the script has been appended already, if yes, don't allow it to run.检查脚本是否已经附加,如果是,则不允许它运行。

if( $('.append').length <= 1 ) {
    $.getJSON('http://example.com?callback=?',function(z){alert(z.query+'Some text');}
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM