getScript-如何仅在尚未调用的情况下调用 [英] getScript - How to only call if not already called

查看:412
本文介绍了getScript-如何仅在尚未调用的情况下调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击一次,将加载以下内容:

on a Click, the following gets loaded:

$.getScript('/javascripts/contacts.js');

我怎么写这样的声明

如果上面的getScript已加载,请执行X;如果尚未加载,请执行Y ...?

if the getScript above is already loaded do X,, if not loaded it, do Y...?

谢谢

推荐答案

var gotContacts = 0;
function getContacts(){
    if (!gotContacts){
        $.getScript('/javascripts/contacts.js', function() {
            gotContacts = 1;
            doSomethingWithContacts();
        });
    }else{
        doSomethingWithContacts();
    }
}

这篇关于getScript-如何仅在尚未调用的情况下调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆