如何仅一次获取脚本 [英] How to getScript only once
问题描述
因此,这是一个简化的问题(我将此作为参考,以使此问题更容易...)
So this is a simplified question of this ( which im using for reference, to make this question easy...)
如果x像素以上,则将大小调整为getScript( jQuery)
如果我需要一个获取脚本的函数,则如果该函数再次触发,它将不会再次加载该脚本.
If I call for a function that gets a script, to, if that function fires again, it will not load the script again.
用^表示.
是一个示例,但是我不久前尝试了该代码.
Is an example, but i tried that code awhile ago.
EX:
$(function()
{
var gotscript=false;
$(window).resize(function()
{
//Dekstop
if (window.innerWidth >= 768)
{
if (!gotscript)
{
// GET DESKTOP SCRIPT TO KEEP THINGS QUICK
$.getScript("js/desktop.js", function()
{
gotscript=true;
});
}
}
if (window.innerWidth < 768)
{
if (window.innerWidth >= 768)
{
if (!gotscript)
{
// GET DESKTOP SCRIPT TO KEEP THINGS QUICK
$.getScript("js/desktop.js", function()
{
gotscript=true;
});
}
}
}
}) .resize(); // trigger resize event
})
这将在任何窗口调整大小事件上加载脚本.
This loads the script on any window resize event.
我忘了提...
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
////// LETS GET SOME THINGS IF IS DESKTOP
var $desktop_load = 0;
if (windowsize >= 768) {
if (!$desktop_load) {
// GET DESKTOP SCRIPT TO KEEP THINGS QUICK
$.getScript("js/desktop.js", function() {
$desktop_load = 1;
});
}
}
////// LETS GET SOME THINGS IF IS MOBILE
if (windowsize < 768) {
}
}
// Execute on load
checkWidth();
这将在窗口大小大于768时起作用,并且由于它仅加载一次此函数,因此不会再次加载脚本.如果小于768,然后超过768,又不再次加载,我该怎么办才能加载脚本.
This will work when window size is above 768, and since its only loading this function once, it will not load the script again. What can i do to make the script load if is less then 768, then goes above 768, and not load it again.
推荐答案
所以没有人想出一个好的答案,也许是我对它的解释不好.无论如何我都解决了自己的问题.
So no one came up with a good answer, maybe it was me not explaining it well. Regardless I solved my own issue.
它将加载脚本@ 768或更高,并且仅加载一次.但是,如果浏览器加载的@小于768,则将浏览器的大小调整为768以上时,它将加载脚本一次.我用它来通过ajax从desktop.js内加载代码块和图像.因此,当它不起作用时,它将重新加载内容,例如,一个加载gif会显示并重置内容.其背后的想法是保持对CSS的响应,并在需要时使用jquery加载更大的循环.
It will load the script @ 768 or above, and only once. But if the browser loads @ less then 768 then the browser is re-sized above 768 it will load the script, once. I use this to load code blocks and images from within desktop.js by ajax. So when it wasnt working, it would re-load the content, an example was a loading gif that would show up and reset the content. The idea behind this was to stay responsive with css, and use jquery to load bigger loops when needed.
$.ajaxSetup({
cache: true
});
var $window = $(window);
var onlyonce = 0;
function resizing() {
var windowsize = $window.width();
if (windowsize >= 768) {
getdeskTop();
}};
function getdeskTop() {
if (onlyonce === 0) {
grabScript ();
} else {
};
};
function grabScript() {
onlyonce = 1;
$.getScript("js/desktop.js", function() { });
}
$(window).resize(resizing);
resizing();
这可能有点丑陋,但我知道它可以工作了:).
This may be a bit ugly, but I got it to work :).
这篇关于如何仅一次获取脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!